Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OSX $ which Python gives /Library/Frameworks/Python.framework/Versions/2.7/bin/python

Hello I'm trying to run twisted along with python but python cannot find twisted.

I did run $pip install twisted successfully but it is still not available.

ImportError: No module named twisted.internet.protocol

It seems that most people have $which python at /usr/local/bin/python

but I get /Library/Frameworks/Python.framework/Versions/2.7/bin/python

May this be the issue? If so, how can I change the PATH env?

like image 336
Nicolas Manzini Avatar asked Apr 27 '14 21:04

Nicolas Manzini


3 Answers

It is just fine. Python may be installed in multiple places in your computer. When you get a new Mac, the default python directory may be

 'usr/bin/python2.7'

You may also have a directory

'System/Library/Frameworks/Python.framework/Versions/2.7/bin/python'

The first one is the symlink of the second one.

If you use HomeBrew to install python, you may get a directory in

'usr/local/bin/python2.7'

You may also have a directory as

'Library/Frameworks/Python.framework/Versions/2.7/bin/python'

which is exactly where my directory is.

The difference between the second one and the fourth one, you may find it here Installing Your Framework

In your question, as you mentioned pip install is successful, but the installed packages still not available. I may guess your pip directory is not in your default python directory, and the packages are installed where your pip directory is. (Please use 'which pip' to check it out)

For example, in my computer, the default pip directory is

/Library/Frameworks/Python.framework/Versions/2.7/bin/pip

though, I have also pip in usr/local/bin.

So, all my packages installed via 'pip install' are stored in

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

Hope that resolves your doubt. Similar things have happened to me, and it took me a whole night to figure out.

Here is the solution: Use PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH" to modify your python directory, or modify your pip directory. However, I would recommend a better way, use virtualenv. This can isolates Python environments, and can help you easily set up packages for each project.

like image 61
Feng Han Avatar answered Oct 19 '22 19:10

Feng Han


By the path your giving for OS X python I'm guessing your a rev-or-so old on your OS X (leopard?) so I can't directly compare with my machine.

But, adding packages to the base OS X install is always a touchy thing, one check I would recommend is the permissions on any packages you add. Do a ls -l /Library/Python/2.7/site-packages/ and make sure everything has r rights (and x rights for directories) (I.E. -rwxr-xr-x or drwxr-xr-x).

I had a recent case where a sudo pip wouldn't set user read rights on installed packages, and I believe "No module" was the error I was getting when I tried to use them

Because adding packages is so touchy on OS X, there are tons of guide on the net to doing hand installs of python. The first one I matched on a google is Installing / Updateing Python on OS X (use at your own risk, I personally haven't followed that guide)

(... the 3rd part install system Brew is a very common method for people to do automated installs of python as well)

like image 31
Mike Lutz Avatar answered Oct 19 '22 19:10

Mike Lutz


Okay well in the terminal I finally found out:

open .bash_profile located at your user root (simply do a $cd in terminal to go there) and add where the path is the location of twisted

PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH" 
export PYTHONPATH
like image 25
Nicolas Manzini Avatar answered Oct 19 '22 19:10

Nicolas Manzini