Python error “ImportError: No module named”

The Question :

495 people think this question is useful

Python is installed in a local directory.

My directory tree looks like this:

(local directory)/site-packages/toolkit/

My code is in here:

(local directory)/site-packages/toolkit/examples/

To run the example, I write python, and in the code I have:

from toolkit.interface import interface

And I get the error:

Traceback (most recent call last):
  File "", line 28, in ?
    from toolkit.interface import interface
ImportError: No module named toolkit.interface

I have already checked sys.path and there I have the directory /site-packages. Also, I have the file in the toolkit folder to indicate to Python that this is a package. I also have a in the examples directory.

I do not know why Python cannot find the file when it is in sys.path. Any ideas? Can it be a permissions problem? Do I need some execution permission?

The Question Comments :
  • Check that you have read permission to that file from python. See:
  • Please be sure to mark your directory as “Resources Root” to let PyCharm know this is a package.
  • The problem in my case was that there was the permission to newly installed modules were not 755. That was because umask on the machine was 0027 due to which the others did not have read permission causing module to not be read. Adding read permission fixed my problem. It’s worth checking the permission of the target directory post-installation.
  • Try to blow url :…
  • maybe local directory name is interface (have been a conflict).

The Answer 1

282 people think this answer is useful

Based on your comments to orip’s post, I guess this is what happened:

  1. You edited on windows.
  2. The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
  3. You used WinSCP to copy the file to your unix box.
  4. WinSCP thought: “This has something that’s not basic text; I’ll put a .bin extension to indicate binary data.”
  5. The missing (now called means python doesn’t understand toolkit as a package.
  6. You create in the appropriate directory and everything works… ?

The Answer 2

77 people think this answer is useful


(local directory)/site-packages/toolkit

have a

To make import walk through your directories every directory must have a file.

The Answer 3

65 people think this answer is useful

I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:

(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)

1) Change directory (cd) to the directory above the directory where your files are. In this case, you’re trying to run the file, and trying to call the module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit directory.

2) When you are in the tookit directory, enter this line of code on your command line:


This sets your PYTHONPATH to “.”, which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn’t just look in your current directory, but in all the directories that are in your current directory).

3) After you’ve set your PYTHONPATH in the step above, run your module from your current directory (the toolkit directory). Python should now find and load the modules you specified.

Hope this helps. I was quite frustrated with this myself.

The Answer 4

41 people think this answer is useful

On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:


(Mind the .: at the beginning, so that it can search on the current directory, too.)

It may also be in other locations, depending on the version:

 .:/usr/lib/python2.7 and etc.

The Answer 5

23 people think this answer is useful

I solved my own problem, and I will write a summary of the things that were wrong and the solution:

The file needs to be called exactly If the extension is different such as in my case .py.bin then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.

Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do which python, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.

The Answer 6

21 people think this answer is useful

Using PyCharm (part of the JetBrains suite) you need to define your script directory as Source:
Right Click > Mark Directory as > Sources Root

The Answer 7

20 people think this answer is useful

an easy solution is to install the module using python -m pip install <library-name> instead of pip install <library-name> you may use sudo in case of admin restrictions

The Answer 8

18 people think this answer is useful

To mark a directory as a package you need a file named, does this help?

The Answer 9

16 people think this answer is useful

You are reading this answer says that your is in the right place, you have installed all the dependencies and you are still getting the ImportError.

I was facing a similar issue except that my program would run fine when ran using PyCharm but the above error when I would run it from the terminal. After digging further, I found out that PYTHONPATH didn’t have the entry for the project directory. So, I set PYTHONPATH per Import statement works on PyCharm but not from terminal:

export PYTHONPATH=$PYTHONPATH:`pwd`  (OR your project root directory)

There’s another way to do this using sys.path as:

import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')

You can use insert/append based on the order in which you want your project to be searched.

The Answer 10

15 people think this answer is useful

For me, it was something really stupid. I installed the library using pip3 install but was running my program as python as opposed to python3

The Answer 11

7 people think this answer is useful

Yup. You need the directory to contain the file, which is the file that initializes the package. Here, have a look at this.

The files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

The Answer 12

6 people think this answer is useful
  1. You must have the file __ in the same directory where it’s the file that you are importing.
  2. You can not try to import a file that has the same name and be a file from 2 folders configured on the PYTHONPATH.

eg: /etc/environment




And, if you are trying to import foo file, python will not know which one you want.

from foo import … >>> importerror: no module named foo

The Answer 13

6 people think this answer is useful

My two cents:

enter image description here


Traceback (most recent call last):
      File "bash\", line 454, in main
        import bosh
      File "Wrye Bash Launcher.pyw", line 63, in load_module
        mod = imp.load_source(fullname,filename+ext,fp)
      File "bash\", line 69, in <module>
        from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
    ImportError: No module named RecordGroups

This confused the hell out of me – went through posts and posts suggesting ugly syspath hacks (as you see my were all there). Well turns out that game/ and game/oblivion was confusing python which spit out the rather unhelpful “No module named RecordGroups”. I’d be interested in a workaround and/or links documenting this (same name) behavior -> EDIT (2017.01.24) – have a look at What If I Have a Module and a Package With The Same Name? Interestingly normally packages take precedence but apparently our launcher violates this.

EDIT (2015.01.17): I did not mention we use a custom launcher dissected here.

The Answer 14

4 people think this answer is useful

Linux: Imported modules are located in /usr/local/lib/python2.7/dist-packages

If you’re using a module compiled in C, don’t forget to chmod the .so file after sudo install.

sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so

The Answer 15

4 people think this answer is useful

To all those who still have this issue. I believe Pycharm gets confused with imports. For me, when i write ‘from namespace import something’, the previous line gets underlined in red, signaling that there is an error, but works. However ”from .namespace import something’ doesn’t get underlined, but also doesn’t work.


    from namespace import something 
except NameError:
    from .namespace import something

The Answer 16

3 people think this answer is useful

In my case, the problem was I was linking to debug python & boost::Python, which requires that the extension be FooLib_d.pyd, not just FooLib.pyd; renaming the file or updating CMakeLists.txt properties fixed the error.

The Answer 17

3 people think this answer is useful

If you have tried all methods provided above but failed, maybe your module has the same name as a built-in module. Or, a module with the same name existing in a folder that has a high priority in sys.path than your module’s.

To debug, say your from import baz complaints ImportError: No module named bar. Changing to import foo; print foo, which will show the path of foo. Is it what you expect?

If not, Either rename foo or use absolute imports.

The Answer 18

3 people think this answer is useful

My problem was that I added the directory with the file to PYTHONPATH, when actually I needed to add its parent directory.

The Answer 19

3 people think this answer is useful

In my case, because I’m using PyCharm and PyCharm create a ‘venv’ for every project in project folder, but it is only a mini env of python. Although you have installed the libraries you need in Python, but in your custom project ‘venv’, it is not available. This is the real reason of ‘ImportError: No module named xxxxxx’ occurred in PyCharm. To resolve this issue, you must add libraries to your project custom env by these steps:

  • In PyCharm, from menu ‘File’->Settings
  • In Settings dialog, Project: XXXProject->Project Interpreter
  • Click “Add” button, it will show you ‘Available Packages’ dialog
  • Search your library, click ‘Install Package’
  • Then, all you needed package will be installed in you project custom ‘venv’ folder.

Settings dialog


The Answer 20

2 people think this answer is useful

Fixed my issue by writing print (sys.path) and found out that python was using out of date packages despite a clean install. Deleting these made python automatically use the correct packages.

The Answer 21

0 people think this answer is useful

After just suffering the same issue I found my resolution was to delete all pyc files from my project, it seems like these cached files were somehow causing this error.

Easiest way I found to do this was to navigate to my project folder in Windows explorer and searching for *.pyc, then selecting all (Ctrl+A) and deleting them (Ctrl+X).

Its possible I could have resolved my issues by just deleting the specific pyc file but I never tried this

The Answer 22

0 people think this answer is useful

I faced the same problem: Import error. In addition the library’ve been installed 100% correctly. The source of the problem was that on my PC 3 version of python (anaconda packet) have been installed). This is why the library was installed no to the right place. After that I just changed to the proper version of python in the my IDE PyCharm.

The Answer 23

0 people think this answer is useful

I had the same error. It was caused by somebody creating a folder in the same folder as my script, the name of which conflicted with a module I was importing from elsewhere. Instead of importing the external module, it looked inside this folder which obviously didn’t contain the expected modules.

The Answer 24

0 people think this answer is useful

I had the same problem (Python 2.7 Linux), I have found the solution and i would like to share it. In my case i had the structure below:


In ‘’ I had tried unsuccessfully all the combinations bellow:

from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...

The solution was much more simple than I thought. I renamed the folder “Booklet” into “booklet” and that’s it. Now Python can import the class Question normally by using in ‘’ the code:

from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass

From this I can conclude that Package-Names (folders) like ‘booklet’ must start from lower-case, else Python confuses it with Class names and Filenames.

Apparently, this was not your problem, but John Fouhy’s answer is very good and this thread has almost anything that can cause this issue. So, this is one more thing and I hope that maybe this could help others.

The Answer 25

0 people think this answer is useful

In my case I was including the path to package.egg folder rather than the actual package underneath. I copied the package to top level and it worked.

The Answer 26

0 people think this answer is useful

This worked for me: Created file inside parent folder (in your case, inside site-packages folder). And imported like this:

from site-packages.toolkit.interface import interface

Hope it will be useful for you as well !

The Answer 27

0 people think this answer is useful

In linux server try dos2unix script_name

(remove all (if there is any) pyc files with command find . -name '*.pyc' -delete)

and re run in the case if you worked on script on windows

The Answer 28

0 people think this answer is useful

In my case, I was using sys.path.insert() to import a local module and was getting module not found from a different library. I had to put sys.path.insert() below the imports that reported module not found. I guess the best practice is to put sys.path.insert() at the bottom of your imports.

The Answer 29

0 people think this answer is useful

If you are using a setup script/utility (e.g. setuptools) to deploy your package, don’t forget to add the respective files/modules to the installer.

When supported, use find_packages() or similar to automatically add new packages to the setup script. This will absolutely save you from a headache, especially if you put your project aside for some time and then add something later on.

import setuptools

    author="Example Author",
    description="A small example package",
        "Programming Language :: Python :: 3",
        "Operating System :: OS Independent",

(Example taken from setuptools documentation)

Add a Comment