roy
roy

Reputation: 3976

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?

Upvotes: 261

Views: 738487

Answers (30)

Dan H
Dan H

Reputation: 14560

I had the same problem: script with import colorama was throwing an 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.

Or 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 into 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.

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

Upvotes: 137

khaverim
khaverim

Reputation: 3544

Been using python 10 years and still this crap happens sometimes. I was using python -m pip install . with a setup.py in the root directory.

I noticed that while the package was being succesfully "installed", a copy of the code was not being added to the environment's site-packages directory, like it normally does for other packages. There was a pkg_name-3.0.1.dist-info/ but no pkg_name/ with the code in it.

I had to change

setup(
    name="pkg_name",
    packages=find_packages('pkg_name'), 
    ...

to

setup(
    name="pkg_name",
    packages=find_packages('.'), 
    ...

in setup.py.

Upvotes: 0

Daniel Ajisafe
Daniel Ajisafe

Reputation: 31

I solved it by calling the following command on the terminal

eval "$(conda shell.bash hook)"

Upvotes: 1

Aramich100
Aramich100

Reputation: 21

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 <yourfile>.py

Make sure you're installing the package on the right Python version.

Upvotes: 2

jens_laufer
jens_laufer

Reputation: 420

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.

Upvotes: 2

Ann
Ann

Reputation: 882

Check that you are using the same python version in the interpreter of your IDE or code editor and on your system. For example, check your python version in the terminal with python3 --version And check python version for interpreter in VSCode by cmd+shift+p-> Python: Select interpreter -> select the same version as you see in your terminal.enter image description here

Upvotes: 9

Not sure if this will help anyone, but I had a similar issue on Mac M1 with zsh. Turns out I had set an alias command in my .zshrc file with the same name as my python command (python3).

To solve the issue, I just had to unalias the command. I ran:

unalias python3

both from my home terminal and from the terminal in Visual Studio.

Upvotes: 1

Mithril
Mithril

Reputation: 13718

Today, I found that package setup.py would produce this problem too.

I have a setup with classifiers < 3

setup(
    name='data_reader',
    version='0.1',

    description='data_reader by Mithril ',
    long_description=long_description,

    author='Mithril',

    classifiers=[
        'Development Status :: 1 - Beta',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',

        'Intended Audience :: Developers',
        'Operating System :: OS Independent',

        "License :: GPLv3",

        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Software Development :: Libraries :: Tools',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],


)

And I was using python 3.7 in a conda env, I found

pip install .
# or
pip install git+https://github.com/eromoe/data_reader

All successed, however import data_reader raise not found .

After some testing, dig that only after changing classifiers to

    classifiers=[
        'Development Status :: 1 - Beta',
        "Programming Language :: Python :: 3",
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        "License :: GPLv3",
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Software Development :: Libraries :: Tools',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],

Install again, the import became normal!

Upvotes: 0

Addison Klinke
Addison Klinke

Reputation: 1176

In my case, I assumed a package was installed because it showed up in the output of pip freeze. However, just the site-packages/*.dist-info folder is enough for pip to list it as installed despite missing the actual package contents (perhaps from an accidental deletion). This happens even when all the path settings are correct, and if you try pip install <pkg> it will say "requirement already satisfied".

The solution is to manually remove the dist-info folder so that pip realizes the package contents are missing. Then, doing a fresh install should re-populate anything that was accidentally removed

Upvotes: 1

Jaikishan
Jaikishan

Reputation: 76

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

Upvotes: 4

Ivan Konovalov
Ivan Konovalov

Reputation: 79

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.

Upvotes: 2

foxof
foxof

Reputation: 11

I know, that this is very old post but I didn't find any answer that was useful in my case (I am using arch linux). I had a similar problem installing "nest_asyncio" package which was definitely installed (visible when listing all the installed packages). There is a right way for arch linux users of installing python packages (as it was already explained here by Emanuel Fontelles). In my case the solution was just to uninstall the remaining not-working package (in my case "nest_asyncio") and then installing it again using the following command:

sudo pacman -S python-"nest_asyncio

That solved all the problems.

Upvotes: 0

Colonna Maurizio
Colonna Maurizio

Reputation: 97

In my case (an Ubuntu 20.04 VM on WIN10 Host), I have a disordered situation with many version of Python installed and variuos point of Shared Library (installed with pip in many points of the File System). I'm referring to 3.8.10 Python version. After many tests, I've found a suggestion searching with google (but' I'm sorry, I haven't the link). This is what I've done to resolve the problem :

  1. From shell session on Ubuntu 20.04 VM, (inside the Home, in my case /home/hduser), I've started a Jupyter Notebook session with the command "jupyter notebook".

  2. Then, when jupyter was running I've opened a .ipynb file to give commands.

  3. First : pip list --> give me the list of packages installed, and, sympy wasn't present (although I had installed it with "sudo pip install sympy" command.

  4. Last with the command !pip3 install sympy (inside jupyter notebook session) I've solved the problem, here the screen-shot : enter image description here

  5. Now, with !pip list the package "sympy" is present, and working : enter image description here

Upvotes: 1

Dima G
Dima G

Reputation: 2025

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'

Upvotes: 1

blackleg
blackleg

Reputation: 361

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

Upvotes: 8

Amit D
Amit D

Reputation: 61

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

File -> Settings -> Project -> Project Interpreter

File -> Invalidate Caches… may be required afterwards.

Upvotes: 6

user1552891
user1552891

Reputation: 567

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

Upvotes: 33

DimiDak
DimiDak

Reputation: 5601

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.

Upvotes: 2

Sludge
Sludge

Reputation: 7385

Simplest solution that worked for me that I don't see mentioned in this thread:

I have multiple versions of Python installed but was trying to use Python3.7 -- so I had to use:

sudo pip3.7 install <package>

Upvotes: -1

RowMow
RowMow

Reputation: 11

As a friend did for me today, here is what helped me (I am using Windows):

Press 'Setting' -> 'Project' -> 'Project Interpreter'. Here in the window on the right, there is a line with the title 'Project Interpreter' on it's left. Click this line and it will open several additional lines.

Now press the 'Show All' line. A window will open. In this window press the small '+' sign in the upper right corner.

A new window will open. On the left there are 4 tabs, press the most upper one, which says 'Virtualenv Environment'. Now, in the window on the right, mark the 'Existing Environment' option. The 'Interpreter' line will become well visible. Press the '...' button on the right of the line.

Now, a browsing window will open. Browse to the directory that you installed Python itself in. Not the one with PyCharm. When you get there, choose the 'python.exe' file and press OK (the window will disappear).

Press OK again (this window will disappear too).

Now in this window make sure the new line you created is marked, and press OK again.

Now, all the installed packages should be visible in the project interpreter, and are read by your program.

Upvotes: 0

Devansh Maurya
Devansh Maurya

Reputation: 1012

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.

Upvotes: 1

Manu
Manu

Reputation: 1

If you are using a virtual environment use pipenv install <module name> instead of pip install <module name>

Worked for me.

Upvotes: -1

Hasnain Haider
Hasnain Haider

Reputation: 1

I have solved my issue that same libraries were working fine in one project(A) but importing those same libraries in another project(B) caused error. I am using Pycharm as IDE at Windows OS. So, after trying many potential solutions and failing to solve the issue, I did these two things (deleted "Venv" folder, and reconfigured interpreter):

1-In project(B), there was a folder named("venv"), located in External Libraries/. I deleted that folder.

2-Step 1 (deleting "venv" folder) causes error in Python Interpreter Configuration, and there is a message shown at top of screen saying "Invalid python interpreter selected for the project" and "configure python interpreter", select that link and it opens a new window. There in "Project Interpreter" drop-down list, there is a Red colored line showing previous invalid interpreter. Now, Open this list and select the Python Interpreter(in my case, it is Python 3.7). Press "Apply" and "OK" at the bottom and you are good to go.

Note: It was potentially the issue where Virtual Environment of my Project(B) was not recognizing the already installed and working libraries.

Upvotes: 0

Moultrie
Moultrie

Reputation: 1

I know this is a super old post but for me, I had an issue with a 32 bit python and 64 bit python installed. Once I uninstalled the 32 bit python, everything worked as it should.

Upvotes: 0

mkst
mkst

Reputation: 674

For me it was ensuring the version of the module aligned with the version of Python I was using.. I built the image on a box with Python 3.6 and then injected into a Docker image that happened to have 3.7 installed, and then banging my head when Python was telling me the module wasn't installed...

36m for Python 3.6 bsonnumpy.cpython-36m-x86_64-linux-gnu.so

37m for Python 3.7 bsonnumpy.cpython-37m-x86_64-linux-gnu.so

Upvotes: 0

Zciurus
Zciurus

Reputation: 838

In my case I had to also install the module(s) for the superuser, too.

sudo su
pip install <module>

Apparently the superuse cannot access the normal users files under certain circumstances.

Upvotes: -1

IShaan
IShaan

Reputation: 93

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.

Upvotes: 3

sbrk
sbrk

Reputation: 1480

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.

Upvotes: 1

Pedram
Pedram

Reputation: 2611

Most of the possible cases have been already covered in solutions, just sharing my case, it happened to me that I installed a package in one environment (e.g. X) and I was importing the package in another environment (e.g. Y). So, always make sure that you're importing the package from the environment in which you installed the package.

Upvotes: 0

Terrence_Freeman
Terrence_Freeman

Reputation: 37

This Works!!!

This often happens when module is installed to an older version of python or another directory, no worries as solution is simple. - import module from directory in which module is installed. You can do this by first importing the python sys module then importing from the path in which the module is installed

import sys
sys.path.append("directory in which module is installed")

import <module_name>

Upvotes: 0

Related Questions