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:

/home/bodacydo
/home/bodacydo/work
/home/bodacydo/work/project
/home/bodacydo/work/project/programs
/home/bodacydo/work/project/foo

And I am in the directory

/home/bodacydo/work/project

Now if I type

python ./programs/my_python_program.py

I instantly get

ImportError: No module named foo.tasks

The ./programs/my_python_program.py contains the following line:

from foo.tasks import my_function

I can't understand why python won't find ./foo/tasks.py - it's there.

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

python
>>> from foo.tasks import my_function

It only doesn't work if I call it via python ./programs/my_python_program.py script.


Solution 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.

Solution 2:

Do you have a file called __init__.py 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.

Solution 3:

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/file.py puts path/to on the beginning of the PYTHONPATH (sys.path).

Solution 4:

Here is a step-by-step solution:

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

    import programs.my_python_program
    programs.my_python_program.main()
    

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

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

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