我有一個具有這種結構的 vscode 專案
- /應用程式
- /勞工
- 組態檔
- 函式.py
- 組態檔
- 測驗.py
- /勞工
注意有兩個組態檔,一個在labor檔案夾下,一個在app檔案夾下。在functions.py中,我試圖在app檔案夾下匯入config.py。
# 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
在 testing.py 中,我正在匯入這兩個檔案
import config as app_config
import labor.config as labor_config
print("test")
這給了我一個 ModuleNotFound 錯誤。最瘋狂的部分是,如果我按住 CTRL 鍵并單擊 functions.py 檔案中 app.config 中的“config”,它會將我帶到該檔案夾??。所以它正在識別它。但不知何故,在運行代碼時,python 不是。
如果您想嘗試一下,我在這里創建了一個 repo:https : //github.com/jadeid91/testproject
uj5u.com熱心網友回復:
你在錯誤的地方運行測驗如果你想在 app 目錄中進行測驗,你必須使用import config而不是import app.config在__init__.pyLabor目錄中的 .py 檔案并添加到 labour 中以確保 python 不會混淆并給你 labour.config
測驗.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)
勞動力/功能.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)
檔案夾結構
- /應用程式
- /勞工
- __init__.py
- 組態檔
- 函式.py
- 組態檔
- 測驗.py
- /勞工
uj5u.com熱心網友回復:
在您的人工檔案夾中添加 __ init__.py 檔案。添加這將使 python 被視為目錄之一。因此,當您匯入時,它會知道在哪里查找并找到該檔案。
您不需要對另一個檔案執行此操作,因為它與您的 testing.py 位于同一目錄中
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/313997.html
