By roy

2013-01-12 17:07:14 8 Comments

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

I have tried installing from pip, easy_install, and via python install from this repo: 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?


@J. Saw 2020-02-03 21:38:05

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>

@roman 2019-12-22 13:02:24

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.

@Aramich100 2019-11-21 20:39:49

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

@Devansh Maurya 2019-11-14 14:14:03

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.

@Manu 2019-10-04 10:08:53

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

Worked for me.

@Walid Bousseta 2020-05-21 12:54:20

pipenv not found

@Hasnain Haider 2019-09-29 12:06:29

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.

@Moultrie 2019-09-24 13:18:50

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.

@streetster 2019-09-18 15:43:36

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

37m for Python 3.7

@streetster 2020-05-21 14:28:44

pip install the version that matches the version of Python you are using. What error do you get, and what is the output of python --version

@Walid Bousseta 2020-05-21 14:36:26

python --version gives me : Python 3.7.7 and when I chek where the package are installed with pip show librosa gives ; Location: /home/ec2-user/anaconda3/lib/python3.7/site-packages how means that the package are in the right place, but when I import the package gives : no module name I am working on the AWS Sagemaker env Pytorch_p36

@streetster 2020-05-22 07:57:35

presumably you want to be using pytorch_p37 ?

@Walid Bousseta 2020-05-22 10:46:58

there's no pytorch_p37

@Zciurus-Alt-Del 2019-09-14 18:50:36

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.

@IShaan 2019-08-19 21:35:37

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.

@Scrotch 2019-08-07 01:29:08

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.

@Pedram 2019-08-02 16:45:14

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.

@Terrence_Freeman 2019-07-04 01:56:18

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>

@aleclara95 2019-05-09 17:41:29

I had a similar problem using Django. In my case, I could import the module from the Django shell, but not from a .py which imported the module.
The problem was that I was running the Django server (therefore, executing the .py) from a different virtualenv from which the module had been installed.

Instead, the shell instance was being run in the correct virtualenv. Hence, why it worked.

@jens_laufer 2019-04-26 09:26:18

In my case it was a problem with a missing 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 file.

@Paul Wang 2013-05-04 17:55:33

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

@jozxyqk 2015-02-16 10:04:20

This happened to me, just running sudo pip install .... Simple fix: sudo chmod -R ugo+rX /lib/python2.7/site-packages/

@Brandyn 2015-03-09 03:30:36

I have run into this many times and I think I just discovered the reason: umask is passed through sudo, so if your personal umask is tight and you use sudo for the install, the files and directories will be over-restricted. If you simply su first and then do the install from a full root shell, the resulting install is usable (at least it is for me). This was for pip, but may apply to apt-get as well. If others confirm this cause, may want to amend the answer accordingly?

@Federico 2016-08-10 05:05:13

Adding to jozxyqk comment's: make sure you got the right directory by doing $ pip show <package_name>

@Antonio Serrano 2018-05-08 16:20:26

Same for me. I am working with Python 3.6 on Mac, so sudo chmod 777 /usr/local/lib/python3.6/site-packages did the trick

@Marx 2020-03-30 12:18:59

sudo 777 is always very bad idea

@Amit Dheemate 2019-02-27 18:40:46

I am new to python. I fixed this issue by changing the project interpreter path.
File -> Settings -> Project -> Project Interpreter

@Christopher Kikoti 2020-06-19 03:06:27

From where, an IDE or???

@huch 2019-01-04 09:32:42

Maybe a bit off-topic, but i had issues to import PyYAML. Points out that you need to import yaml. (guess it's a classical rtfm...)

@blackleg 2018-09-04 05:56:08

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.

@Dima G 2018-05-07 06:42:49

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

@MJ_ 2018-04-01 20:19:26

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.

@Chris 2017-04-06 14:05:07

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.

@Patrick B. 2017-01-10 07:09:29

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 "", line 2, in <module>
    from telegram.ext import Updater
  File "$USER/", 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 And right, I had a script lying there called and this was loaded by python when I called import.

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

@George Weber 2016-12-06 16:32:31

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

(did work)

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


(did not work)


@user1552891 2014-04-22 17:45:22

It's the python path problem.

In my case, I have python installed in:


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

The package(SOAPpy) I installed by pip is located


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

@edst 2016-09-15 06:16:01

This worked for me in getting Jenkins working with selenium python.

@Shivam Sharma 2017-07-31 18:28:48

This worked for me in running celery through supervisor, when my bash script called my celery script; celery script couldn't import the already installed modules inside my pip. Thanks a ton.

@Sanchit 2020-02-21 09:10:48

Note: another possibility of the import error is that you have multiple versions of Python installed in your computer. For example, one from the system's Python and one from Miniconda or Anaconda. Therefore, be sure always to use the correct corresponding "pip".

@Locane 2016-06-21 22:32:23

I have been banging my head against my monitor on this until a young-hip intern told me the secret is to "python 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
[burnc7 (2016-06-21 15:28:54) foo]# python install

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


python /root/foo/ install


cd /root/foo
python install

@Dan H 2016-01-14 19:12:47

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.

@Graydyn Young 2017-07-14 15:59:14

alternatively you can use sudo's -H flag

@ivanlan 2016-04-17 19:08:39

I had this exact problem, but none of the answers above worked. It drove me crazy until I noticed that sys.path was different after I had imported from the parent project. It turned out that I had used importlib to write a little function in order to import a file not in the project hierarchy. Bad idea: I forgot that I had done this. Even worse, the import process mucked with the sys.path--and left it that way. Very bad idea.

The solution was to stop that, and simply put the file I needed to import into the project. Another approach would have been to put the file into its own project, as it needs to be rebuilt from time to time, and the rebuild may or may not coincide with the rebuild of the main project.

@Ryan Artecona 2013-01-12 17:26:56

When you install via easy_install or pip, is it completing successfully? What is the full output? Which python installation are you using? You may need to use sudo before your installation command, if you are installing modules to a system directory (if you are using the system python installation, perhaps). There's not a lot of useful information in your question to go off of, but some tools that will probably help include:

  • echo $PYTHONPATH and/or echo $PATH: when importing modules, Python searches one of these environment variables (lists of directories, : delimited) for the module you want. Importing problems are often due to the right directory being absent from these lists

  • which python, which pip, or which easy_install: these will tell you the location of each executable. It may help to know.

  • Use virtualenv, like @JesseBriggs suggests. It works very well with pip to help you isolate and manage the modules and environment for separate Python projects.

@Ali Afshar 2013-01-12 17:19:35

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

@Martijn Pieters 2013-01-12 17:21:53

It can't be option 3 in this case; that would not throw an ImportError.

@roy 2013-01-12 17:59:17

maybe 2? I just don't know why it would install in the wrong place for this one library, where most others are fine.

@Dan H 2016-04-26 21:37:32

Option 4: Your PYTHONPATH is right, your library is where you want it, you don't have a library masking this... but because you used sudo pip install it got installed with root privs, and at import time something is bombing for the lack of read or write access.

@Dan H 2016-04-26 21:38:44

Not quite true, @MartijnPieters: if there was an import of the form "from <package> import <foo>", and if the obstructing package had no "foo", then you'd get an import error for option 3.

@Martijn Pieters 2016-04-26 23:03:41

@DanH sure, but that's not the form the OP is using.

@Abdulrahman Bres 2019-07-01 15:25:55

Option 3 is totally valid!

Related Questions

Sponsored Content

31 Answered Questions

[SOLVED] How to import a module given the full path?

38 Answered Questions

[SOLVED] How to install pip on Windows?

28 Answered Questions

[SOLVED] Importing files from different folder

30 Answered Questions

[SOLVED] How can I get a list of locally installed Python modules?

  • 2009-04-11 12:34:18
  • Léo Léopold Hertz 준영
  • 1316939 View
  • 1010 Score
  • 30 Answer
  • Tags:   python module pip

14 Answered Questions

[SOLVED] Calling a function of a module by using its name (a string)

  • 2008-08-06 03:36:08
  • ricree
  • 729956 View
  • 1788 Score
  • 14 Answer
  • Tags:   python object

13 Answered Questions

[SOLVED] Python DNS module import error

22 Answered Questions

[SOLVED] Import a module from a relative path

2 Answered Questions

[SOLVED] jenkins python module import error

1 Answered Questions

[SOLVED] how to disable pypy assert statement?

  • 2012-12-27 07:57:38
  • peerxu
  • 254 View
  • 5 Score
  • 1 Answer
  • Tags:   python pypy

1 Answered Questions

[SOLVED] what are the rules for imports in python

Sponsored Content