Python 2.7.3 on OSX 10.8.2
I'm currently writing a script that imports the markdown module. I used the #!/usr/bin/env python shebang for portability. The script runs fine when I run it directly in shell via ./myscript.py arg1
When I run the script from outside of a (login) shell, for example via AppleScript do shell script "/path/to/myscript.py " & quoted form of arg1, it fails with
myscript.py", line 8, in <module> import markdown ImportError: No module named markdown I guess that this might be a problem with the shebang, so I changed the shebang to my python location #!/usr/local/bin/python and sure enough the script worked fine.
So my question twofold:
- Why does using
/usr/bin/env pythonbreak my import? - How can I avoid this problem without having to use
/usr/local/bin/python?
/usr/bin/env pythonas much as using a non-login shell. I'm assuming that/usr/local/binisn't onPATHin such a shell, or at least not before/usr/bin, which would explain whypythonin the latter is picked. Are you changingPATHin your bash config files?#!/usr/bin/env pythonand#!/usr/local/bin/pythonwere working perfectly in TextWrangler, and both gave me identical results when run in Terminal. It was only calling it from AppleScript that produced the error (rodrigo is correct). While TextWrangler and Terminal both gave me identical results, I was able to see the difference by runningdo shell script "(echo 'import sys'; echo 'print sys.path';) | /usr/bin/env python"in AppleScript Editor.do shell script "which python"would have been even easier.