Python
Error Handling
ModuleNotFoundError
Debugging
Python Packages

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:

bash
python -m pip install <package-name>

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:

text
1project/
2├── main.py
3└── utils/
4    ├── __init__.py
5    └── helpers.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:

bash
source venv/bin/activate        # Windows: venv\Scripts\activate
python -m pip install <package-name>
python -c "import sys; print(sys.executable)"

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:

bash
PYTHONPATH=/path/to/project python main.py

Or, as a last resort inside the script:

python
1import sys
2sys.path.append("/path/to/project")
3
4import utils

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 venv or virtualenv to isolate dependencies for each project.
  • Debugging import errors: use verbose import options or an IDE to see where import paths fail.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms