How do I set up a local python library directory / PYTHONPATH?
Question: In the process of trying to write a Python script that uses PIL today, I
discovered I don't seem have it on my local machine (OS X 10.5.8, default 2.5
Python install).

So I run:

    
    
    easy_install --prefix=/usr/local/python/ pil
    

and it complains a little about /usr/local/python/lib/python2.5/site-packages
not yet existing, so I create it, and try again, and get this:

> TEST FAILED: /usr/local/python//lib/python2.5/site-packages does NOT support
> .pth files error: bad install directory or PYTHONPATH
>
> You are attempting to install a package to a directory that is not on
> PYTHONPATH and which Python does not read ".pth" files from. The
> installation directory you specified (via --install-dir, --prefix, or the
> distutils default setting) was:
>  
>  
>     /usr/local/python//lib/python2.5/site-packages
>  
>
> and your PYTHONPATH environment variable currently contains:
>  
>  
>     ''
>  

OK, fair enough -- I hadn't done anything to set the path. So I add a quick
line to ~/.bash_profile:

> PYTHONPATH="$PYTHONPATH:/usr/local/python/lib/python2.5"

and `source` it, and try again.

Same error message.

This is kindof curious, given that PYTHONPATH is clearly set; I can `echo
$PYTHONPATH` and get back `:/usr/local/python/lib/python2.5`. I decided to
check out what the include path looked like from inside:

    
    
    import sys
    print "\n".join(sys.path)
    

which yields:

> /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
> /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
> /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-
> darwin
> /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-
> mac
> /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-
> mac/lib-scriptpackages
> /System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
> /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-
> tk
> /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-
> dynload /Library/Python/2.5/site-packages
> /System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC

from which `/usr/local/python/yadda/yadda` is notably missing.

Not sure what I'm supposed to do here. How do I get python to recognize this
location as an include path?

**UPDATE**

As Sven Marnach suggested, I was neglecting to export PYTHONPATH. I've
corrected that problem, and now see it show up when I print out `sys.path`
from within Python. However, I still got the `TEST FAILED` error message I
mentioned above, just with my new PYTHONPATH environment variable.

So, I tried changing it from `/usr/local/python/lib/python2.5` to
`/usr/local/python/lib/python2.5/site-packages`, exporting, and running the
same `easy_install` command again. This leads to an all new result that at
first _looked_ like success (but isn't):

    
    
    Creating /usr/local/python/lib/python2.5/site-packages/site.py
    Searching for pil
    Reading http://pypi.python.org/simple/pil/
    Reading http://www.pythonware.com/products/pil
    Reading http://effbot.org/zone/pil-changes-115.htm
    Reading http://effbot.org/downloads/#Imaging
    Best match: PIL 1.1.7
    Downloading http://effbot.org/media/downloads/PIL-1.1.7.tar.gz
    Processing PIL-1.1.7.tar.gz
    Running PIL-1.1.7/setup.py -q bdist_egg --dist-dir /var/folders/XW/XWpClVq7EpSB37BV3zTo+++++TI/-Tmp-/easy_install-krj9oR/PIL-1.1.7/egg-dist-tmp--Pyauy
    --- using frameworks at /System/Library/Frameworks
    [snipped: compiler warnings]
    --------------------------------------------------------------------
    PIL 1.1.7 SETUP SUMMARY
    --------------------------------------------------------------------
    version       1.1.7
    platform      darwin 2.5.1 (r251:54863, Sep  1 2010, 22:03:14)
                  [GCC 4.0.1 (Apple Inc. build 5465)]
    --------------------------------------------------------------------
    --- TKINTER support available
    --- JPEG support available
    --- ZLIB (PNG/ZIP) support available
    *** FREETYPE2 support not available
    *** LITTLECMS support not available
    --------------------------------------------------------------------
    To add a missing option, make sure you have the required
    library, and set the corresponding ROOT variable in the
    setup.py script.
    
    To check the build, run the selftest.py script.
    zip_safe flag not set; analyzing archive contents...
    Image: module references __file__
    No eggs found in /var/folders/XW/XWpClVq7EpSB37BV3zTo+++++TI/-Tmp-/easy_install-krj9oR/PIL-1.1.7/egg-dist-tmp--Pyauy (setup script problem?)
    

Again, this looks good, but when I go to run my script:

> Traceback (most recent call last):  
>  File "checkerboard.py", line 1, in import Image, ImageDraw ImportError: No
> module named Image

When I check what's now under `/usr/local/python/` using `find .`, I get:

> ./lib ./lib/python2.5 ./lib/python2.5/site-packages ./lib/python2.5/site-
> packages/site.py ./lib/python2.5/site-packages/site.pyc

So... nothing module-looking (I'm assuming site.py and site.pyc are metadata
or helper scripts). Where did the install go?

I note this:

> To check the build, run the selftest.py script.

But don't really know what that is.

And I also noticed the "No eggs found" message. Are either of these hints?


Answer: You are using the Apple-supplied Python 2.5 in OS X; it's a framework build
and, by default, uses `/Library/Python/2.5/site-packages` as the location for
installed packages, not `/usr/local`. Normally you shouldn't need to specify
`--prefix` with an OS X framework build. Also beware that the `setuptools`
(`easy_install`) supplied by Apple with OS X 10.5 is also rather old as is the
version of Python itself.

That said, installing `PIL` completely and correctly on OS X especially OS X
10.5 is not particularly simple. Search the archives or elsewhere for tips
and/or binary packages. Particularly if you are planning to use other modules
like MySQL or Django, my recommendation is to install everything (Python and
PIL) using a package manager like [MacPorts](http://www.macports.org/).

