In this post, you will learn the solutions to solve the error Modulenotfounderror: no module named ‘src’.
Actually, all programmers in python may encounter this error because the Python interpreter cannot find a module that your code is trying to import from a subdirectory named “src”
Also read the other solved error in python: Modulenotfounderror: no module named dotenv [SOLVED]
What is src in Python?
In Python, “src” is not a built-in keyword or function. It is mostly used as a shortcut of “source”, which refers to the source code of a program or module.
What are the possible reasons for this error no module named src?
The possible reasons for this error no module named src which are the following:
- The module you are trying to import does not exist.
- The module is located in a subdirectory named “src” and you forget to include the subdirectory in the import statement.
- The module is located in a virtual environment or in a conda environment which is not currently activated.
- The subdirectory named “src” does not exist in the directory where your Python script is located.
- The subdirectory named “src” exists, but it is not on your Python path.
- You misspelled the name of the module when you were trying to import it.
- The module that you are trying to import is haven’t been installed on your system.
- You are running your script from a different directory than the one where the script and “src” directory are located.
How to solve the error Modulenotfounderror: no module named ‘src’?
Time needed: 3 minutes
Here are the following solutions to solve the error Modulenotfounderror: no module named ‘src’
- Step 1: Check the Import the module name correctly.
First, Double-check that the module you are trying to import exists and that you have spelled the module name correctly.
- Step 2: Check if the ‘src’ is in the correct directory
Second, Check if the module is located in a subdirectory named “src” and include the subdirectory in the import statement.
Add the “src” directory to your Python path.
- Step 3: Activate the correct virtual environment
Third, Activate the correct virtual environment or conda environment if the module is located in an environment.
- Step 4: Module ‘src’ is installed in the correct environment.
Last but not the least, If you are using a package manager like pip or conda.
Make sure that the module is installed and that you have installed it in the correct environment.
Diagnostic checklist for “No module named ‘src'”
- Verify pip install target. Run
pip show src— if not installed, runpip install src. - Check the active Python interpreter.
which python(mac/Linux) orwhere python(Windows). Both pip and python must point to the same environment. - Check virtual environment activation. If you use venv/conda, activate before installing:
source .venv/bin/activate. - Rule out uppercase/lowercase. Python imports are case-sensitive:
import PyPDF2notimport pypdf2. - Rule out the pip-vs-package-name mismatch. Some packages install under a different name than you import (e.g.
pip install beautifulsoup4→import bs4).
Installing src
# Standard pip install pip install src # In a virtual environment (recommended) python -m venv .venv source .venv/bin/activate # or .venv\Scripts\activate on Windows pip install src # With uv (faster alternative) uv pip install src
Common causes for “No module named ‘src'”
- Python interpreter mismatch. Multiple Python installations can confuse pip. Verify with
which pythonandwhich pip. - Virtual environment not activated. If the venv isn’t activated, pip installs to your system Python instead.
- Notebook kernel mismatch. Jupyter uses a kernel that may differ from your terminal Python. Use
%pip install srcinside the notebook. - Import name differs from install name. pip install beautifulsoup4 → import bs4. Check the package’s PyPI page.
- Windows PATH issues. Ensure Python is on PATH; use
python -m pip installto invoke pip via the correct Python.
Working code example
# Verify install worked import src print(getattr(src, '__version__', 'no version attribute'))
Best practices
- Always use a virtual environment. Avoids most module-not-found errors.
- Use pip freeze to lock versions.
pip freeze > requirements.txtmakes your setup reproducible. - Consider uv or Poetry. Modern package managers with better dep resolution and reproducibility.
Official documentation
Quick step-by-step summary (click to expand)
- Add an __init__.py to the src folder. Create an empty file src/__init__.py so Python treats the directory as a package.
- Add the project root to PYTHONPATH. Run PYTHONPATH=. python your_script.py from the project root, or add sys.path.insert(0, str(Path(__file__).parent.parent)) at the top of your script.
- Use a src layout with a proper pyproject.toml. Configure setuptools or hatch to recognize the src layout. Add [tool.setuptools.packages.find] where=[‘src’] to pyproject.toml.
- Install your package in editable mode. Run pip install -e . in the project root. Once installed, from src.module import foo works from anywhere.
Frequently Asked Questions
What is Python ModuleNotFoundError and what causes it?
ModuleNotFoundError (a subclass of ImportError) is raised when Python cannot find the module you tried to import. Common causes: the package isn’t installed (pip install missing), wrong virtual environment activated, typo in module name, or Python can’t find your local module on the import path. The error message names exactly which module is missing.
How do I fix ‘ModuleNotFoundError: No module named X’?
Run pip install X first. If that succeeds but you still get the error, check which Python you’re using (which python OR python –version) vs which pip (which pip OR pip –version), they must match. Common gotcha: pip points to system Python 3.9 but you’re running python3.11 in a venv. Inside the venv, use python -m pip install X to be sure pip matches the active Python.
Why does my code work in one environment but not another?
Different Python versions or different installed packages. To diagnose: pip freeze > requirements.txt on the working environment, then pip install -r requirements.txt on the broken one. Use virtualenv (python -m venv venv) or conda for every project to avoid system-wide package collisions.
Is ModuleNotFoundError the same as ImportError?
ModuleNotFoundError is a subclass of ImportError added in Python 3.6. It specifically means ‘no such module exists.’ Plain ImportError covers a wider set: module exists but a name inside it can’t be imported (e.g. ‘cannot import name X from Y’). except ImportError catches both; except ModuleNotFoundError catches only the missing-module case.
Where can I find more ModuleNotFoundError fixes?
Browse the ModuleNotFoundError reference hub for 198+ specific module fixes (TensorFlow, Flask, Django, pandas, numpy, etc.). For related issues see ImportError. For broader Python setup see Python Tutorial hub.
Conclusion
To conclude, by following the steps above, you should be able to resolve the error ModuleNotFoundError: No module named src.
For identifying the specific reason why the no module named src error occurred.
You can take the applicable solution above to resolve to error you encountered
