Fix ModuleNotFoundError: No module named 'utils' in Python
Codemia Knowledge Hub ·
ModuleNotFoundError: No module named 'utils' means the Python interpreter cannot find a module called utils that your script imports. In most projects utils is a local module holding shared helper functions or classes, so the cause is usually a naming, installation, structure, environment or path problem.
Why Python cannot find utils
1. Incorrect module name
A typo or wrong name in the import is the simplest cause. Check that the name is spelled correctly and matches the case of the actual file or package.
2. Module not installed
utils might be an external library. If it is not part of the Python Standard Library, install it with a package manager such as pip before importing it.
3. Incorrect file structure
Python locates modules through the directory hierarchy. If the project's directories are not set up correctly, the interpreter cannot find the module.
4. Virtual environment issues
In a virtual environment, required packages must be installed inside that environment, not globally. Otherwise you get ModuleNotFoundError.
5. Path issues
The script may run from a directory where utils is not accessible, for example because the IDE or script sets the wrong working directory.
How to fix it
Correct the module name
Use the exact module name in the import. Python import statements are case-sensitive.
Install the package
If utils comes from a third-party package, install it with the same interpreter that runs your script:
Use the package's real name from its documentation; the import name and the name on PyPI are not always the same.
Fix the project structure
For a local utils package, keep it inside the project and give it an __init__.py:
Run python main.py from the project folder, and from utils.helpers import ... resolves because Python adds the script's folder to the import path. If the script lives in a subfolder of a package, run it as a module from the project root instead, for example python -m myproject.scripts.run.
Install into the active virtual environment
Activate the environment before installing and running, then confirm which interpreter is in use:
The printed path should point inside the venv folder. In an IDE, select that same interpreter for the project.
Fix the path
If the module lives outside the script's folder, add its parent directory to the search path:
Or, as a last resort inside the script:
Setting PYTHONPATH or running with python -m from the project root is cleaner than editing sys.path in code.
Habits that prevent it
- Documentation and resources: check the documentation of the module you are importing. It often covers installation and sample usage.
- Managing multiple environments: use
venvorvirtualenvto isolate dependencies for each project. - Debugging import errors: use verbose import options or an IDE to see where import paths fail.
Related reading
- ModuleNotFoundError What does it mean __main__ is not a package?
- MongoKit vs MongoEngine vs Flask-MongoAlchemy for Flask
- Most efficient way to map function over numpy array
- Most efficient way to reverse a numpy array
- mongod, mac os x - rlimits warning
- MongoDB - The argument to size must be an Array, but was of type EOO / missing
- Most idiomatic way to convert None to empty string?
- Most pythonic way to delete a file which may not exist
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.