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

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

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.

Do you have a file called __init__.py in the foo directory? If not then python wont recognise foo as a python package.

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

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

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

Leave a Reply

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