Reputation: 2016
I have the following structure:
app/
code/
script.py -- has a function called func
main.py
How can I import script.py
from main.py
?
I tried from code.script import func
and I got ModuleNotFoundError: No module named 'code.script'; 'code' is not a package
Upvotes: 0
Views: 56
Reputation: 4328
Indeed the best way is to add __init__.py
in code
directory because when a regular package is imported __init__.py
file is implicitly executed and the objects it defines are bound to names in the package’s namespace.
FYI, as an alternative you can also to this in your main.py
before the import:
import sys
sys.path.append("/path/to/script.py")
Upvotes: 0
Reputation: 398
Place a __init__.py
file in the code
directory. This will allow your main.py
code to import it as a module like you have tried there.
Upvotes: 4