I have the following project structure:
- project
- src
- module_a
- foo.py
- __init__.py
- runner.py
- constants.py
- __init__.py
constants.py:
SOME_CONST = 5
foo.py:
from src.constants import SOME_CONST
class Foo:
def some_func(self):
....
runner.py:
from src.module_a.foo import FOO
def main():
Foo.some_func()
if __name__ == "__main__":
main()
Trying to run python runner.py
from the src folder gave me a ModuleNotFound
about src
when foo.py
was trying to import from src.constants import SOME_CONST
.
I understand that the issue was the python interpreter didn’t recognise the src
folder as a module, even though it has an __init__.py
file.
After creating a venv virtual environment the issue was resolved.
I have the following questions:
- Why didn’t the python interpreter recognise the
src
folder as a module before creating a virtual environment? - Why did creating a virtual environment solve the issue?
- How can I set up the project so that it will run without setting a virtual environment?
Thanks!
Eyal Golan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.