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

巧了我就是萌 提交于 2019-12-07 05:25:40

问题


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?


回答1:


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.




回答2:


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)




回答3:


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



回答4:


I too was getting a ImportError: No module named xxxeven though I did a pip install xxx and pip2 install xxx. pip2.7 install xxx worked for me. This installed it in the python 2.7 directory.



来源:https://stackoverflow.com/questions/23329034/python-osx-which-python-gives-library-frameworks-python-framework-versions-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!