ModuleNotFoundError: no module named ‘src’

Python developers often face various issues while working with Python. Because Python has extensive libraries that offer a powerful and flexible environment for developers. One common problem is when you see a message that says, “ModuleNotFoundError: No module named ‘src.’” In this article, we will explain why this error occurs and provide appropriate solutions to fix it.

What is the “ModuleNotFoundError: No module named ‘src'”?

The “ModuleNotFoundError: No module named ‘src’” error can occur from various factors. This error message indicates that your Python code is trying to import a module named ‘src.’ But Python can’t find it in the current environment. Resolving the “ModuleNotFoundError: No module named ‘src’” error involves a systematic approach.

By meticulously following these steps and utilizing appropriate codes, developers can effectively overcome this error. Thus, they can ensure a seamless execution of their Python projects. As with any programming challenge, a thorough understanding of the potential causes, along with systematic problem-solving strategies, is critical to achieving successful resolutions.

What Causes the “ModuleNotFoundError: No module named ‘src'”?

The ModuleNotFoundError: No module named ‘src’ can be caused due to several factors. Understanding these causes is crucial for efficiently resolving the issue.

Incorrect Module Path

One of the primary reasons for this error is an incorrect module path. If the ‘src’ module is not located in a directory that is part of the Python path, the interpreter won’t be able to find it.

# Incorrect module path
from src import module_name


ModuleNotFoundError: no module named 'src'
ModuleNotFoundError: no module named 'src'

Missing File

If the ‘src’ directory is intended to be treated as a package, it must contain an ‘’ file. This file can be empty, but its presence signifies that the directory should be recognized as a Python package.

# Directory structure
- project_folder
- src

Virtual Environment Mismatch

In projects utilizing virtual environments, discrepancies between the project’s environment and the virtual environment can lead to module import issues.

# Activate virtual environment
!source venv/bin/activate

How to Resolve the “ModuleNotFoundError: No module named ‘src'”?

Addressing the “ModuleNotFoundError: No module named ‘src’” error involves systematically tackling the identified causes. Below are detailed steps and code snippets to help resolve this issue.

Correct Module Path

Ensure that the module path is correctly specified, considering the project structure. If ‘src’ is a subdirectory, the correct import statement would be:

# Correct module path
from project_folder.src import module_name

Add File

Create an ‘’ file inside the ‘src’ directory. This will indicate that the file is a Python package. This step is essential when ‘src’ is intended to be treated as a package.

# Directory structure
- project_folder
- src

Activate Virtual Environment

Ensure that the virtual environment is activated and matches the project requirements. To activate the virtual environment, you try running the following command:

# Activate virtual environment
!source venv/bin/activate

Environment Variable Configuration

Another way to resolve the “ModuleNotFoundError: No module named ‘src’” is to set the PYTHONPATH environment variable. It will include the path to the project’s ‘src’ directory. It ensures that Python can locate the required modules without manual intervention.

import os
os.environ["PYTHONPATH"] = f"{os.environ['PYTHONPATH']}:/path/to/your/project/"

Utilizing .env Files

Another solution to resolve this error is to create a ‘.env’ file in your project directory and add the following line to it:

import os
os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + ':' + os.getcwd()

Then, source the ‘.env’ file in the terminal to set the PYTHONPATH dynamically.


Why does the error mention ‘src’ specifically?

The ‘src’ mentioned in the error is the name of the module or package that the Python interpreter cannot find. If you encounter this error, it means that you are trying to import or reference a module named ‘src,’ but Python cannot locate it.

Is it necessary to have an ‘’ file in the ‘src’ directory?

Yes, it is necessary to have an ‘‘ file in the ‘src‘ directory if you intend to treat it as a Python package. This file signals to Python that the directory should be recognized as a package, allowing for proper module imports.

Can this error occur in a virtual environment?

Yes, the “ModuleNotFoundError: No module named ‘src’” error can occur in a virtual environment. It is crucial to activate the virtual environment and ensure that it aligns with the project’s requirements.


In conclusion, the “ModuleNotFoundError: No module named ‘src’” error is a common hurdle for Python developers, often stemming from incorrect module paths, missing ‘’ files, or virtual environment mismatches.

By carefully addressing these causes and following the provided solutions, developers can overcome this error and ensure the smooth execution of their Python projects. As with any programming challenge, a systematic and methodical approach is critical to effective problem-solving.


  1. ModuleNotFoundError
  2. src

To learn more about errors in Python, head to Errors Section and follow PythonClear to know more of Python related modules and errors.

Leave a Comment