Getting PATH right for python after MacPorts install

15,354

Solution 1

Use the 'notes' command after installing python (change python26 to whatever version you installed):

$ port notes python26

You'll see some variation on post install recommendations:

python26 has the following notes:
  To fully complete your installation and make python 2.6 the default,  please
  run:
    sudo port install python_select
    sudo port select python python26

You should install python_select and then select python26 as your default Python. Then, if you did indeed install PIL and pyscopg2 using MacPorts (i.e. something like port install py26-psycopg2 py26-pil), then you shouldn't have to specify anything in PYTHONPATH. You also don't need to add the /opt/local/Library/Frameworks/… directories to your PATH. After doing this, which python should return /opt/local/bin/python. Python ports installed by MacPorts in /opt/local/Library/Frameworks/Python.framework are already in the sys.path for MacPorts Python. You were not finding them earlier because you were using the System Python (i.e. /usr/bin/python), which has no knowledge of MacPorts Python packages.

Solution 2

I think I figured out the answer to my question. I edited my ~/.bash_profile file to include

export PYTHONPATH=

and then I added all of the existing paths in sys.path:

export PYTHONPATH=/Library/Frameworks/SQLite3.framework/Versions/3/Python:/Library/Python/2.6/site-packages/numpy-override:/Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages:/Library/Frameworks/cairo.framework/Versions/1/Python:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages:/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload:/Library/Python/2.6/site-packages:/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC:/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages:

and then I appended the paths to the directories where MacPorts installed stuff:

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages:

now when I print sys.path, the paths to the MacPorts install directories are included:

/Library/Frameworks/SQLite3.framework/Versions/3/Python
/Library/Python/2.6/site-packages/numpy-override
/Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages
/Library/Frameworks/cairo.framework/Versions/1/Python
/Library/Frameworks/SQLite3.framework/Versions/3/Python
/Library/Python/2.6/site-packages/numpy-override
/Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages
/Library/Frameworks/cairo.framework/Versions/1/Python
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/Library/Python/2.6/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages

Now

>>> import PIL

and

>>> import psycopg2

work just fine.

Share:
15,354

Related videos on Youtube

BenjaminGolder
Author by

BenjaminGolder

Updated on September 17, 2022

Comments

  • BenjaminGolder
    BenjaminGolder over 1 year

    I can't import some python libraries (PIL, psycopg2) that I just installed with MacPorts. I looked through these forums, and tried to adjust my PATH variable in ~/.bash_profile in order to fix this but it did not work.

    I added the location of PIL and psycopg2 to PATH.
    I know that Terminal is using a version of python in /usr/local/bin rather than the one installed by MacPorts at /opt/local/bin.

    Do I need to use the MacPorts version of Python in order to ensure that PIL and psycopg2 are on sys.path when I use python in Terminal? Should I switch to the MacPorts version of Python, or will that cause more problems?

    In case it is helpful, here are more facts:

    PIl and psycopg2 are installed in /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages

    which pythonreturns/usr/bin/python

    $ echo $PATHreturns (I separated each path for easy reading):

    :/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
    :/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
    :/opt/local/bin
    :/opt/local/sbin
    :/usr/local/git/bin
    :/usr/bin
    :/bin
    :/usr/sbin
    :/sbin
    :/usr/local/bin
    :/usr/local/git/bin
    :/usr/X11/bin
    :/opt/local/bin
    

    in python, >>> sys.path returns:

    /Library/Frameworks/SQLite3.framework/Versions/3/Python
    /Library/Python/2.6/site-packages/numpy-override
    /Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages
    /Library/Frameworks/cairo.framework/Versions/1/Python
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
     /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
    /Library/Python/2.6/site-packages
    /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC
    /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode
    

    I welcome any criticism and comments, if any of the above looks foolish or poorly conceived. I'm new to all of this. Thanks!

    Running OSX 10.6.5 on a MacBook Pro, invoking python 2.6.1 from Terminal

  • BenjaminGolder
    BenjaminGolder over 13 years
    Unfortunately, this doesn't really resolve the issue of having multiple versions of the same library. If there is a library with the same name in two directories, and both are in PATH, how does one know which will be used?
  • BenjaminGolder
    BenjaminGolder over 13 years
    Found a book on UNIX and shell programming, which said that when given a command the shell will search through the directories in PATH according to the order that they are listed. In other words the first directory listed when you type echo $PATH will be searched first. I assume the same is true of PYTHONPATH.
  • BenjaminGolder
    BenjaminGolder over 13 years
    Thank you fideli. Just tried it. It was super easy and totally worked. That is the right way to do it.
  • kalaracey
    kalaracey almost 12 years
    Why does macports require that you select this python? Why is it not selected by itself (atleast on my machine, I installed python for the first time under macports; shouldn't it have automatically selected that?)
  • fideli
    fideli almost 12 years
    @kalaracey MacPorts attempts to be non-intrusive when there are obvious conflicts, i.e. MacPorts vs System Python. If you would like to use python26 installed through MacPorts, you must select it using python-select. Note that using Python with the full path /opt/local/bin/python2.6 works as well all the time.
  • Deliss
    Deliss about 11 years
    These days it looks like: $ port notes python27 sudo port select --set python python27