Module Not Found Error: No module named 'src'
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.
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/
You can add a path to python runtime using sys.path:
import sys
sys.path.append('src/package1')
import script1