Reputation: 3951
From main.py, I want to import a file from the backend folder
WebAppName/main.py WebAppName/backend/handlers.py
How do I specify this as an import statement
I am aware that importing from the same folder is just import handlers
But this is a child directory, so how do I do this?
Upvotes: 0
Views: 243
Reputation: 4304
When you do an import, Python searchs for whatever you are importing in the directories listed in sys.path, which is a Python list. To make a module or other code source importable, simply append the path to the code source to sys.path:
sys.path.append(os.path.join(os.path.abspath('.'), 'backend'))
After that line, then do your import of handlers and it will work.
good luck, Mike
Upvotes: 2
Reputation: 3981
so you are importing handlers.py in main.py?
import backend.handlers as handlers
should work if you put an __init__.py
in backend
EDIT: option 2
in the beginning of main.py you could add the child folder to your python path by doing something like:
import sys
sys.path.append('./backend')
Upvotes: 1
Reputation: 38930
You need to have an __init__.py
file in the backend
folder for Python to consider it a package. Then you can do import backend.handlers
or from backend.handlers import foo
Upvotes: 2