Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 309
roy Avatar asked Jan 12 '13 17:01

roy


People also ask

Why can't I import modules in Python?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

Why PyCharm Cannot import module?

Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.


19 Answers

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

like image 110
Paul Wang Avatar answered Oct 08 '22 14:10

Paul Wang


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.

like image 36
Dan H Avatar answered Oct 08 '22 14:10

Dan H


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

like image 29
user1552891 Avatar answered Oct 08 '22 13:10

user1552891


I was able to correct this issue with a combined approach. First, I followed Chris' advice, opened a command line and typed 'pip show packagename' This provided the location of the installed package.

Next, I opened python and typed 'import sys', then 'sys.path' to show where my python searches for any packages I import. Alas, the location shown in the first step was NOT in the list.

Final step, I typed 'sys.path.append('package_location_seen_in_step_1'). You optionally can repeat step two to see the location is now in the list.

Test step, try to import the package again... it works.

The downside? It is temporary, and you need to add it to the list each time.

like image 24
MJ_ Avatar answered Oct 08 '22 15:10

MJ_


The Python import mechanism works, really, so, either:

  1. Your PYTHONPATH is wrong,
  2. Your library is not installed where you think it is
  3. You have another library with the same name masking this one
like image 21
Ali Afshar Avatar answered Oct 08 '22 14:10

Ali Afshar


I have been banging my head against my monitor on this until a young-hip intern told me the secret is to "python setup.py install" inside the module directory.

For some reason, running the setup from there makes it just work.

To be clear, if your module's name is "foo":

[burnc7 (2016-06-21 15:28:49) git]# ls -l
total 1
drwxr-xr-x 7 root root  118 Jun 21 15:22 foo
[burnc7 (2016-06-21 15:28:51) git]# cd foo
[burnc7 (2016-06-21 15:28:53) foo]# ls -l
total 2
drwxr-xr-x 2 root root   93 Jun 21 15:23 foo
-rw-r--r-- 1 root root  416 May 31 12:26 setup.py
[burnc7 (2016-06-21 15:28:54) foo]# python setup.py install
<--snip-->

If you try to run setup.py from any other directory by calling out its path, you end up with a borked install.

DOES NOT WORK:

python /root/foo/setup.py install

DOES WORK:

cd /root/foo
python setup.py install
like image 27
Locane Avatar answered Oct 08 '22 15:10

Locane


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
  1. I used sudo's -H option and reinstalled keyring:
  • sudo -H pip install keyring
like image 34
blackleg Avatar answered Oct 08 '22 14:10

blackleg


In PyCharm, I fixed this issue by changing the project interpreter path.

File -> Settings -> Project -> Project Interpreter

File -> Invalidate Caches… may be required afterwards.

like image 37
Amit D Avatar answered Oct 08 '22 15:10

Amit D


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
like image 39
George Weber Avatar answered Oct 08 '22 14:10

George Weber


Something that worked for me was:

python -m pip install -user {package name}

The command does not require sudo. This was tested on OSX Mojave.

like image 23
IShaan Avatar answered Oct 08 '22 13:10

IShaan


This problem can also occur with a relocated virtual environment (venv).

I had a project with a venv set up inside the root directory. Later I created a new user and decided to move the project to this user. Instead of moving only the source files and installing the dependencies freshly, I moved the entire project along with the venv folder to the new user.

After that, the dependencies that I installed were getting added to the global site-packages folder instead of the one inside the venv, so the code running inside this env was not able to access those dependencies.

To solve this problem, just remove the venv folder and recreate it again, like so:

$ deactivate
$ rm -rf venv
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
like image 40
Jaikishan Avatar answered Oct 08 '22 13:10

Jaikishan


In my case I had run pip install Django==1.11 and it would not import from the python interpreter.

Browsing through pip's commands I found pip show which looked like this:

> pip show Django
Name: Django
Version: 1.11
...
Location: /usr/lib/python3.4/site-packages
...

Notice the location says '3.4'. I found that the python-command was linked to python2.7

/usr/bin> ls -l python
lrwxrwxrwx 1 root root 9 Mar 14 15:48 python -> python2.7

Right next to that I found a link called python3 so I used that. You could also change the link to python3.4. That would fix it, too.

like image 21
Chris Avatar answered Oct 08 '22 14:10

Chris


In my case it was a problem with a missing init.py file in the module, that I wanted to import in a Python 2.7 environment.

Python 3.3+ has Implicit Namespace Packages that allow it to create a packages without an init.py file.

like image 44
jens_laufer Avatar answered Oct 08 '22 14:10

jens_laufer


I had colorama installed via pip and I was getting "ImportError: No module named colorama"

So I searched with "find", found the absolute path and added it in the script like this:

import sys
sys.path.append("/usr/local/lib/python3.8/dist-packages/")
import colorama 

And it worked.

like image 31
DimiDak Avatar answered Oct 08 '22 14:10

DimiDak


I had just the same problem, and updating setuptools helped:

python3 -m pip install --upgrade pip setuptools wheel

After that, reinstall the package, and it should work fine :)

The thing is, the package is built incorrectly if setuptools is old.

like image 33
Ivan Konovalov Avatar answered Oct 08 '22 15:10

Ivan Konovalov


If the other answers mentioned do not work for you, try deleting your pip cache and reinstalling the package. My machine runs Ubuntu14.04 and it was located under ~/.cache/pip. Deleting this folder did the trick for me.

like image 31
Scrotch Avatar answered Oct 08 '22 14:10

Scrotch


Also, make sure that you do not confuse pip3 with pip. What I found was that package installed with pip was not working with python3 and vice-versa.

like image 31
Devansh Maurya Avatar answered Oct 08 '22 13:10

Devansh Maurya


Had this problem too.. the package was installed on Python 3.8.0 but VS Code was running my script using an older version (3.4)

fix in terminal:

py .py

Make sure you're installing the package on the right Python Version

like image 26
Aramich100 Avatar answered Oct 08 '22 15:10

Aramich100


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'
like image 25
Dima G Avatar answered Oct 08 '22 13:10

Dima G