Unable to import a module that is definitely installed


After installing mechanize, I don't seem to be able to import it.

I have tried installing from pip, easy_install, and via python setup.py install from this repo: https://github.com/abielr/mechanize. All of this to no avail, as each time I enter my Python interactive I get:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mechanize

The installations I ran previously reported that they had completed successfully, so I expect the import to work. What could be causing this error?

1/21/2019 7:01:29 PM

Accepted Answer

In my case, it is permission problem. The package was somehow installed with root rw permission only, other user just cannot rw to it!

3/3/2019 6:10:00 PM

I had the same problem: script with import colorama was throwing and ImportError, but sudo pip install colorama was telling me "package already installed".

My fix: run pip without sudo: pip install colorama. Then pip agreed it needed to be installed, installed it, and my script ran.

My environment is Ubuntu 14.04 32-bit; I think I saw this before and after I activated my virtualenv.

UPDATE: even better, use python -m pip install <package>. The benefit of this is, since you are executing the specific version of python that you want the package in, pip will unequivocally install the package in to the "right" python. Again, don't use sudo in this case... then you get the package in the right place, but possibly with (unwanted) root permissions.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow