roy roy - 16 days ago 8
Python Question

Cannot import a python module that is definitely installed (mechanize)

On-going woes with the python (2.7.3) installation on my Ubuntu 12.04 machine and importing modules.

Here I am having an issue where I have definitely installed mechanize both on my machine and in various virtual environments.

I have tried installing from pip, easy_install and via

python setup.py install
from this repo: https://github.com/abielr/mechanize.

To no avail, each time, I enter my python interactive and 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
>>>


Other computers I install this on do not have a problem (a mac, or a windows machine at work, for instance, it's all good, installs and imports like normal).

It's pretty much driving me crazy at this point, just want to get some work done.

UPDATE INFO (in response to comments):

Out put of
easy_install mechanize
and paths:

<me>@<host>:~$ sudo easy_install mechanize
[sudo] password for <me>:
Processing mechanize
Writing /home/<me>/mechanize/setup.cfg
Running setup.py -q bdist_egg --dist-dir /home/<me>/mechanize/egg-dist-tmp-zXwJ_d
warning: no files found matching '*.html' under directory 'docs'
warning: no files found matching '*.css' under directory 'docs'
warning: no files found matching '*.js' under directory 'docs'
mechanize 0.2.6.dev-20130112 is already the active version in easy-install.pth

Installed /usr/local/lib/python2.7/dist-packages/mechanize-0.2.6.dev_20130112-py2.7.egg
Processing dependencies for mechanize==0.2.6.dev-20130112
Finished processing dependencies for mechanize==0.2.6.dev-20130112
<me>@<host>:~$ ^C
<me>@<host>:~$ which pip
/home/<me>/bin/pip
<me>@<host>:~$ which python
/home/<me>/bin/python
<me>@<host>:~$ which easy_install
/home/<me>/bin/easy_install
<me>@<host>:~$


SECOND UPDATE:
Seems to be something with mechanize, if I add any other random package via PIP, there is not problem (in this instance
html5lib
)

THIRD UPDATE (@DSM)

1)
>>> sys.path
['', '/home/<me>/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', '/home/<me>/local/lib/python2.7/site-packages/virtualenvwrapper-2.11-py2.7.egg', '/home/<me>/src/geopy', '/home/<me>/local/lib/python2.7/site-packages/BeautifulSoup-3.2.0-py2.7.egg', '/home/<me>/local/lib/python2.7/site-packages/django_sorting-0.1-py2.7.egg' ... <so on and so forth but mechanize is not here>]
>>>

2) *pretty long output of which most looks like:*
<me>@<host>:~$ ls -laR /usr/local/lib/python2.7/dist-packages/mech*
/usr/local/lib/python2.7/dist-packages/mechanize:
total 1144
...lots of other files, pretty much same permissions...
-rw-r--r-- 1 root staff 24916 Jan 11 01:19 _mechanize.py
...lots of other files...

3)
>>> import imp
>>> imp.find_module("mechanize")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
>>>


FOURTH EDIT (this is getting ridiculous :/):
This is similar to a problem I've had before (Complete removal and fresh install of python on Ubuntu 12.04), if I run everything with sudo, it's fine, but I don't know if I should have to do that...what's up with the permissions?

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!