I have a minimal directory with the following structure:
- my_project
- code
- my_code.py
- functions
- module1.py
- code
The contents of each file are:
## module1.py
def add_three(number):
return number+3
And
## my_code.py
from functions.module1 import add_three
result_1 = add_three(1)
print(result_1)
In the command line I cd
to the my_project
directory, then I run python -m code.my_code
and I get this:
C:[my_user]AppDataLocalMicrosoftWindowsAppsPythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0python.exe: Error while finding module specification for 'code.my_code' (ModuleNotFoundError: __path__ attribute not found on 'code' while trying to find 'code.my_code')
Most answers I’ve seen for this error suggest not to include the .py
extension with the -m
flag, but of course I’ve already done that.
What’s funny is if I cd ..
, change the import to from my_project.functions.module1 import add_three
, and run python -m my_project.code.my_code
, it runs correctly.