How to fix “ImportError: No module named …” error in Python?

Posted on

Question :

How to fix “ImportError: No module named …” error in Python?

What is the correct way to fix this ImportError error?

I have the following directory structure:


And I am in the directory


Now if I type

python ./programs/

I instantly get

ImportError: No module named foo.tasks

The ./programs/ contains the following line:

from foo.tasks import my_function

I can’t understand why python won’t find ./foo/ – it’s there.

If I do it from the Python shell, then it works:

>>> from foo.tasks import my_function

It only doesn’t work if I call it via python ./programs/ script.

Asked By: bodacydo


Answer #1:

Python does not add the current directory to sys.path, but rather the directory that the script is in. Add /home/bodacydo/work/project to either sys.path or $PYTHONPATH.

Answer #2:

Do you have a file called in the foo directory? If not then python won’t recognise foo as a python package.

See the section on packages in the python tutorial for more information.

Answered By: Dave Kirby

Answer #3:

Here is a step-by-step solution:

  1. Add a script called in /home/bodacydo/work/project and edit it like this:

    import programs.my_python_program

    (replace main() with your equivalent method in my_python_program.)

  2. Go to /home/bodacydo/work/project
  3. Run

Since python appends to PYTHONPATH the path of the script from which it runs, running will append /home/bodacydo/work/project. And voilà, import foo.tasks will be found.

Answered By: cyborg

Answer #4:

Example solution for adding the library to your PYTHONPATH.

  1. Add the following line into your ~/.bashrc or just run it directly:

    export PYTHONPATH="$PYTHONPATH:$HOME/.python"
  2. Then link your required library into your ~/.python folder, e.g.

    ln -s /home/user/work/project/foo ~/.python/
Answered By: kenorb

Answer #5:

A better fix than setting PYTHONPATH is to use python -m module.path

This will correctly set sys.path[0] and is a more reliable way to execute modules.

I have a quick writeup about this problem, as other answerers have mentioned the reason for this is python path/to/ puts path/to on the beginning of the PYTHONPATH (sys.path).

Answered By: Anthony Sottile

Answer #6:

In my mind I have to consider that the foo folder is a stand-alone library. I might want to consider moving it to the Libsite-packages folder within a python installation. I might want to consider adding a foo.pth file there.

I know it’s a library since the ./programs/ contains the following line:

from foo.tasks import my_function

So it doesn’t matter that ./programs is a sibling folder to ./foo. It’s the fact that is run as a script like this:

python ./programs/

Answered By: quamrana

Answer #7:

If you have this problem when using an instaled version, when using, make sure your module is included inside packages

setup(name='Your program',
    description='Your desccription',
    packages=['foo', ''], # add `` here
Answered By: Danon

Leave a Reply

Your email address will not be published. Required fields are marked *