Cannot import a python module that is definitely i

2019-01-02 20:32发布

Ongoing 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.

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
>>> 

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, and I 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, as if I add any other random package via pip, there is no 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), where 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?

15条回答
孤独总比滥情好
2楼-- · 2019-01-02 20:58

I encountered this while trying to use keyring which I installed via sudo pip install keyring. As mentioned in the other answers, it's a permissions issue in my case.

What worked for me:

  1. Uninstalled keyring:
    • sudo pip uninstall keyring
  2. I used sudo's -H option and reinstalled keyring:
    • sudo -H pip install keyring

Hope this helps.

查看更多
听够珍惜
3楼-- · 2019-01-02 21:03

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

查看更多
闭嘴吧你
4楼-- · 2019-01-02 21:04

I had similar problem (on Windows) and the root cause in my case was ANTIVIRUS software! It has "Auto-Containment" feature, that wraps running process with some kind of a virtual machine. Symptoms are: pip install somemodule works fine in one cmd-line window and import somemodule fails when executed from another process with the error

ModuleNotFoundError: No module named 'somemodule'

I hope it will save some time to somebody :)

查看更多
深知你不懂我心
5楼-- · 2019-01-02 21:06

It's the python path problem.

In my case, I have python installed in:

/Library/Frameworks/Python.framework/Versions/2.6/bin/python,

and there is no site-packages directory within the python2.6.

The package(SOAPpy) I installed by pip is located

/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/

And site-package is not in the python path, all I did is add site-packages to PYTHONPATH permanently.

  1. Open up Terminal
  2. Type open .bash_profile
  3. In the text file that pops up, add this line at the end:

    export PYTHONPATH=$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/

  4. Save the file, restart the Terminal, and you're done
查看更多
伤终究还是伤i
6楼-- · 2019-01-02 21:09

I couldn't get my PYTHONPATH to work properly. I realized adding export fixed the issue:

(did work)

export PYTHONPATH=$PYTHONPATH:~/test/site-packages

vs.

(did not work)

PYTHONPATH=$PYTHONPATH:~/test/site-packages
查看更多
深知你不懂我心
7楼-- · 2019-01-02 21:09

I had this problem with 2.7 and 3.5 installed on my system trying to test a telegram bot with Python-Telegram-Bot.

I couldn't get it to work after installing with pip and pip3, with sudo or without. I always got:

Traceback (most recent call last):
  File "telegram.py", line 2, in <module>
    from telegram.ext import Updater
  File "$USER/telegram.py", line 2, in <module>
    from telegram.ext import Updater
ImportError: No module named 'telegram.ext'; 'telegram' is not a package

Reading the error message correctly tells me that python is looking in the current directory for a telegram.py. And right, I had a script lying there called telegram.py and this was loaded by python when I called import.

Conclusion, make sure you don't have any package.py in your current working dir when trying to import. (And read error message thoroughly).

查看更多
登录 后发表回答