I have a vscode project with this structure
- /app
- /labor
- config.py
- functions.py
- config.py
- testing.py
- /labor
Notice that there are two config files, one under the labor folder and one under the app folder. In functions.py I'm trying to import config.py under the app folder.
# note I need to add app. before config because if I don't it will think that I'm referencing the file under labor folder
import app.config as app_config
In testing.py I'm importing both files
import config as app_config
import labor.config as labor_config
print("test")
This is giving me a ModuleNotFound error. The crazy part is, if I hold my CTRL key and click on "config" in app.config in the functions.py file, it will take me to the folder. So it is recognizing it. But somehow, when running the code, python isn't.
I created a repo here if you'd like to try it out: https://github.com/jadeid91/testproject
CodePudding user response:
you running testing in wrong place
if you want testing in app directory you have to use import config
instead of import app.config
in labor directory .py files and add __init__.py
to labor to make sure python dont confused and give you labor.config
testing.py
import config as app_config
import labor.config as labor_config
import labor.functions as labor_functions
print(app_config.MAIN_TABLE_ID)
print(labor_config.LABOR_TABLE_ID)
labor/functions.py
import config as app_config
# if I import config, it will take me to labor.config
# import config
def hello():
print(app_config.MAIN_TABLE_ID)
folder structure
- /app
- /labor
- __init__.py
- config.py
- functions.py
- config.py
- testing.py
- /labor
CodePudding user response:
add __ init__.py file in your labor folder. Adding this will make python consider as 1 of the directories. so when you import, it will know where to look for and finds the file.
You need not do this for the other file as it is in the same directory as your testing.py