質問

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?
役に立ちましたか?

解決

#!/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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top