Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Module imports in Terminal but not through Unix Shell

I'm just getting started with Python and need a little guidance.

I'm using a Mac, but I have a python.org build of python installed as well as the original Apple install.

I'm trying to load up some 3rd party modules. When I run the script in IDLE or through Terminal, everything works fine. When I try to run it as a CRON job, I get an error saying it can't find the 3rd party module.

After some poking around, I've been lead to believe it's the PYTHONPATH / sys.path. I created a test script to show me my path. So why do I get different paths when it's run through terminal window versus directly as a shell script?

My two questions are:

  1. Why are they different?
  2. How can I get the direct shell process to find the 3rd Party Modules?

Here's my sys.path output when I run it in Terminal:

['/Library/Scripts',
 '/Library/Scripts/$',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/Library/Frameworks/Python.framework/Versions/7.3/lib/python27.zip',
 '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7',
 '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-darwin',
 '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-mac',
 '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk',
 '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-old',
 '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-dynload',
 '/Library/Python/2.7/site-packages']

Here's my sys.path output when I run it in the shell (in this case I'm in Applescript with a "do shell script" step.

"['/Library/Scripts',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages']"
like image 997
user1766887 Avatar asked Oct 06 '22 05:10

user1766887


1 Answers

Why are they different?

Because you're running two different Python interpreters, and they have different default sys.path values.

That's on purpose—it's what allows you to modify the standard (or site) library of your Python.org installation, without breaking the system installation (or any of the Apple-supplied tools that depend on that installation).

I'm guessing that when you run python in a Terminal, you're getting /usr/local/bin/python (you can check that by typing which python), which is presumably a link to /Library/Frameworks/Python.framework/Versions/7.3/bin/python (or possibly to a Python.app/Contents/MacOS/python somewhere inside that framework). And that Python, from python.org, is compiled to set up sys.path around /Library/Frameworks/Python.framework/Versions/7.3/.

Meanwhile, when you run python via Launch Services, you're probably getting /usr/bin/python, which is (hopefully) the built-in shim around /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python, which is compiled to set up sys.path around /System/Library/Frameworks/Python.framework/Versions/2.7/.

The reason python does different things in those cases is that /usr/local/bin is not on your login PATH at all, but is added to the head of the path by a line in your ~/.profile (or other) file. That file is read by shell sessions in Terminal, but not by Launch Services sessions.

If you want to use the same Python in all environments, you could explicitly run /usr/local/bin/python or /usr/bin/python instead of just python. (You could also get /usr/local/bin onto your login PATH, but that's a very bad idea, or get it out of your ~/.profile, but that's a little inconvenient.)

As a side note, this is a sign that your python.org installation is very broken:

/Library/Frameworks/Python.framework/Versions/7.3/lib/python27.zip

There is no Python 7.3. I'd strongly consider uninstalling and reinstalling the python.org build.

Or, more simply, uninstall it and leave it uninstalled, and just use the built-in Python 2.7. If you want multiple environments, use virtualenv instead of multiple installations. If, e.g., Apple's giving you 2.7.2 and you really really need 2.7.3, then you need another installation; otherwise, you're just making things harder for no benefit.

like image 155
abarnert Avatar answered Oct 10 '22 02:10

abarnert