| |
| | |

Apple Phoon Forum

 Forgot password?
 Register
View: 2298|Reply: 15

Automator, python and MySQL

[Copy link]

Post time: 2013-12-27 21:18:18 |Show all posts
AppleScript,  Unix
I'm using Automator to create a service that parses the contents of a text file, then places the results into a MySQL database. The service consists of one command: "Run Shell Script", where the script is a python file that takes as it's argument the text file that has been used to call the service. The problem I'm coming up against is that the import MySQLdb command in the python file causes the service to fail. Is there some compatibility issue that I'm not aware of concerning MySQL and Automator? The script works fine from the command line, and Automator seems to be using the same 32-bit version of Python 2.7 that the is invoked on the command line.
Any thoughts much appreciated.
        Automator, Mac OS X (10.7.5)
Reply

Use magic Report

Post time: 2013-12-28 18:29:01 |Show all posts
Frank,
There is so much stuff in Mountain Lion...
Not surprising ... same readlink output as you here too. D'oh. However, if you change it slightly, it points to the Frameworks location:
If I invoke the Python 2.5 interactive environment:
Reply

Use magic Report

Post time: 2013-12-28 16:50:29 |Show all posts
You're correct, went back to look on both my 10.8.3 systems and it is there. Don;t know I missed it the first time.
However the output for the three commands Hiroto posted remain the same as in my post. Not sure what the significance of that is.
Thanks for the correction,
regards
Reply

Use magic Report

Post time: 2013-12-28 14:51:35 |Show all posts
Not only is there a Python.Framework within /System/Library/Frameworks on 10.7.5, 10.8.2, and 10.8.3; there are individual Python 2.5, 2.6, and 2.7 framework subfolders that contain respective Python executables with version specific libraries. There is a fourth folder named Current that links to the 2.7 folder.
In /usr/bin, there are the following executables, all but python itself are called python2.{5,6,7} that link to their respective Python.framework/Versions/2.{5,6,7} folders and respective executable found there. In /usr/lib, this same pattern emerges for the libraries that link back to the the preceding folders and version specific lib folder.
If I wanted to go interactive with Python 2.5.6 on 10.8.3, I would invoke it as /usr/bin/python2.5.
Reply

Use magic Report

Post time: 2013-12-28 13:11:41 |Show all posts
Hello
My pleasure. Glad to know it helped.
As for the reported python's executable path, I don't know why it is like that. Under 10.6.5, though, I get the same result as Frank Caggiano. So it appears to have changed around 10.6.
However, Activity Monitor under 10.6.5 still reports the following files, not limited to, are open when /usr/bin/python is running in a shell -
/System/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python/System/Library/Frameworks/Python.framework/Versions/2.6/Python
So..., although sys.executable reports /usr/bin/python under 10.6, the actual executable(s) may be different.
---
Back to the original issue, in order for MySQLdb module to work, mysql's library path needs to be exported in $DYLD_LIBRARY_PATH.
E.g., assuming it resides in /usr/local/mysql/lib,
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
And it is likely done in your ~/.bash_profile, I guess.
All the best,
H
Reply

Use magic Report

Post time: 2013-12-28 11:57:48 |Show all posts
Thanks Hiroto,
Adding the source line to my shell script has solved the problem. I still don't quite understand what's going on under the hood, since sys. executable is still reported as the OSX version of Python, even though it has no access to MySQL. I'm beginning to think this variable might not reflect that actual version of python being used, since a number of other threads note that on occassion the sys module doesn't behave exactly as one might expect!
Reply

Use magic Report

Post time: 2013-12-28 10:43:35 |Show all posts
On 10.8.3 the only python is in /usr/bin. There is nothing in /System/Library/Frameworks
Reply

Use magic Report

Post time: 2013-12-28 09:34:28 |Show all posts
Hello
/usr/bin/python seems to execute Python.app/Contents/MacOS/Python under 10.5.8 as well.
$ which python/usr/bin/python$ readlink /usr/bin/python../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python$ /usr/bin/python -c 'import sys; print sys.executable'/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python
So I'd think the executable's path is not the problem especially since you have no problem in running the code in command line.
Most likely cause is having different environments, specifically $PATH etc, in shell run from Automator. You need to source appropriate profile in your shell script in Automator, such as -
source ~/.bash_profile/path/to/your/programme.py "$@"
provided that your environments are set in ~/.bash_profile.
Hope this may help,
H
Reply

Use magic Report

Post time: 2013-12-28 08:19:40 |Show all posts
What's in process_txt._file.py that is the file the shell is running. After that it is all your script.
what the #! line in your script calling for?
Reply

Use magic Report

Post time: 2013-12-28 07:18:23 |Show all posts
Again, Automator doesn't choose anything, but there may be symlinks in the framework (the /Library/Frameworks/Python.framework is not a standard install) or something set up in the python paths.
Reply

Use magic Report

Post time: 2013-12-28 05:40:05 |Show all posts
I think I get what you're saying, about running a default shell, but I thought I was using full paths. The python I want to use is at /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-32, and I specified this both at the top of my python file, and as the first argument in the 'Run Shell Script' action, but Automater nevertheless chooses something different. Anyway, thanks for the help, and I'll try to change the environment from within my shell.
Reply

Use magic Report

Post time: 2013-12-28 04:23:30 |Show all posts
The Run Shell Script action in Automator doesn't choose any specific version of Python (or anything else for that matter), but it is running a default shell which does not use any settings that you may have made to your bash environment. You will need to make changes to the environment from within your shell, or just work with the default settings (e.g. use full paths).
Reply

Use magic Report

Post time: 2013-12-28 03:03:32 |Show all posts
It's beginning to look like the problem is being caused by Automator choice of Python. Although I have tried specifying the complete path to my python executable within the 'Run Shell Script' text, if I add a print sys.executable command to the python file that is called by the service, I get a different path.
Text of "Run Shell Script":
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-32 my_file.py "$@"
Output of line print sys.executable within my_file.py:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/ MacOS/Python
This latter version of Python has not been hooked up to MySQL, which is the most likely cause of my problem. So, why is Automator choosing not to use the version of Python I specify? Does automator have environmental variables that differ from the bash environment? If it does, can I influence them?
Reply

Use magic Report

Post time: 2013-12-28 01:49:33 |Show all posts
Th Automator Run Shell Script action just uses a default shell, so depending on where your modules are located you may need to use the full path.
Reply

Use magic Report

Post time: 2013-12-28 00:05:14 |Show all posts



As requested, here is a screenshot of the workflow I'm having problems with.
Reply

Use magic Report

Post time: 2013-12-27 22:43:52 |Show all posts
Can you upload a photo of the workflow? That would help.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Archive| Apple Phoon

2021-04-18 06:02 GMT-8 , Processed in 0.088115 sec., 27 queries .

Powered by Discuz! 7.2

Release 20121101, © 2001-2021

To Top