11

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?

Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81
  • 2
    You're probably doomed. Re-install from OS vendor installation material and next time only install new Python package using virtualenv. – Jean-Paul Calderone Apr 28 '14 at 01:02
  • You don't have nearly enough information for a real answer here. At a minimum, please include your OS X version (the output of `sw_vers` in Terminal) your Python version and build information (the output you get when you just type `python` in a Terminal) and a list of any Python packages you have installed with system installers (`pkgutil --pkgs | grep -i python` should approximate that). – Glyph Apr 29 '14 at 08:29

4 Answers4

17

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.

Feng Han
  • 361
  • 2
  • 7
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)

Mike Lutz
  • 1,812
  • 1
  • 10
  • 17
  • 1
    actually what solved the problem for me was to add to .bash_profile PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH" export PYTHONPATH – Nicolas Manzini Apr 27 '14 at 22:45
1

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
Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81
  • 3
    Watch out as you continue to install python stuff on that machine, that isn't the normal install directory for the pre-installed OS X python, so whichever install tools your using may get more and more confused with time. If your going to keep working on there you would likely be best off setting up a virtual env on a hand install python. – Mike Lutz Apr 27 '14 at 22:56
0

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.

sand
  • 137
  • 1
  • 2
  • 9