Reputation: 594
Whenever I run main.py
in the terminal, I get the error
ModuleNotFoundError: No module named 'src'
However it runs fine in PyCharm.
Project Structure:
-project
-resources
-src
-package1
-script1.py
-script2.py
-package2
-script3.py
-main.py
This is what I run in the terminal:
project$ python src/main.py
Error:
Traceback (most recent call last):
File "src/main.py", line 1, in <module>
from src.package1 import script1
ModuleNotFoundError: No module named 'src'
I have already tried adding absolute path of folder/package 'src' to sys.path
main.py
from src.package1 import script1
from src.package1 import script2
from src.package2 import script3
if name=="__main__":
...
...
sys.path
current sys.path is ['/home/xyz/Projects/project/src', '/home/xyz/Apps/anaconda3/envs/project/lib/python37.zip', '/home/xyz/Apps/anaconda3/envs/project/lib/python3.7', '/home/xyz/Apps/anaconda3/envs/project/lib/python3.7/lib-dynload', '/home/xyz/Apps/anaconda3/envs/project/lib/python3.7/site-packages', 'src/main.py']
Upvotes: 16
Views: 54793
Reputation: 1
In my case it was interfering with src folder from another project, set in environnent variables.
Upvotes: 0
Reputation: 720
There are chances you encountered such error because you're following the guidelines that told you to put your code in src/
folder AND you are using PyCharm IDE. It was my case, at least.
My configuration was all good (I'm using pyproject.toml
), except for one thing... In PyCharm, when importing an internal module I would get something like
from src.my_package.utils.log import log_d
The src
part was the source of the problem, but without it PyCharm wouldn't know how to find the modules.
In PyCharm Preferences
I went to the menu Project: <my_project>
/Project Structure
.
Right-click on the src/
folder you see in the right panel, choose it as Sources
.
Rename all your internal references without the src.
part
from my_package.utils.log import log_d
Test, rebuild, it should be OK now.
Upvotes: 3
Reputation: 129
You should be able to run it with project$ python -m src.main
Source: https://qavalidation.com/2021/03/how-to-resolve-modulenotfounderror-no-module-named-src.html/
Upvotes: 12
Reputation: 396
You can add a path to python runtime using sys.path:
import sys
sys.path.append('src/package1')
import script1
Upvotes: 6
Reputation: 13225
https://docs.python.org/3/tutorial/modules.html#the-module-search-path
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named
spam.py
in a list of directories given by the variablesys.path
.sys.path
is initialized from these locations:
- The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH
(a list of directory names, with the same syntax as the shell variable PATH).- The installation-dependent default.
Since you supply a file, src/main.py
, its containing folder is going to be the search root. You could import the modules without specifying the src.
part.
Upvotes: 14