我有一個關于我的新 python 專案的問題。這是我第一次為我的專案使用不同的檔案夾。
我有以下結構:
project
src
securityFunc
__init__.py
createCredentialsXML.py
main.py
我在 PyCharm 環境中作業。按播放后,我收到錯誤訊息:
Traceback (most recent call last):
File "C:\project\src\main.py", line 1, in <module>
from securityFunc import *
File "C:\project\src\securityFunc\__init__.py", line 1, in <module>
from createCredentialsXML import *
ModuleNotFoundError: No module named 'createCredentialsXML'
我的主要功能如下所示:
from securityFunc import *
if __name__ == '__main__':
generate_key()
__init__.py:
from createCredentialsXML import *
createCredentialsXML.py:
def generate_key():
key = base64.urlsafe_b64encode(os.urandom(2048))
with open("../key/secret.key", "wb") as key_file:
key_file.write(key)
我嘗試使用 Path 或 sys.path 來解決問題。但它不起作用。
你能告訴我如何解決這個問題嗎?
uj5u.com熱心網友回復:
createCredentialsXML.py在模塊中作業securityFunc;您必須指定匯入的范圍。使用
from securityFunc.createCredentialsXML import *
在securityFunc.__init__.py應該作業。
uj5u.com熱心網友回復:
由于您正在進行相對匯入,因此需要添加一個 . 在您的模塊名稱之前:
from .createCredentialsXML import *
點表示該模塊與匯入代碼的代碼位于同一目錄中。
你可以在這里閱讀更多關于它的資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/394641.html
上一篇:RestAPI過濾器引數JSON
