Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using /usr/bin/env break my Python import?

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:

  1. Why does using /usr/bin/env python break my import?
  2. How can I avoid this problem without having to use /usr/local/bin/python?
like image 245
n8henrie Avatar asked Apr 07 '13 23:04

n8henrie


2 Answers

#!/usr/bin/env python means "go find python on $PATH as if it the shell were looking for it, and run that." So since you get different results, you are probably using different pythons.

To check, see if running /usr/local/bin/python and /usr/bin/env python give you the same pythons. You can also use type -a python to find every python on $PATH. On my system, type -a python gives:

python is /opt/local/bin/python
python is /usr/bin/python
python is /usr/local/bin/python

(That first one is installed by MacPorts.)

Anyway, as rodrigo points out, direct launching is probably not using the $PATH you expect. Which means using /usr/bin/env isn't going to work.

like image 50
Mike DeSimone Avatar answered Sep 20 '22 08:09

Mike DeSimone


Well, it looks like the PATH environment variable in your login shell is different than in the AppleScript process.

My guess is that you have a .profile file or similar with the line:

PATH=/usr/local/bin:$PATH

But that file is only executed if you open a shell, not from other processes. And obviously you have a different version of Python in /usr/bin/python that does not have the markdown module.

like image 32
rodrigo Avatar answered Sep 20 '22 08:09

rodrigo