環境:
- 蟒蛇:3.9
- PyCharm 下的 Virtualenv
在我正在撰寫的當前源代碼中,我嘗試:
- 動態檢查是否安裝了所需的依賴項
- 如果不是,則會出現一條訊息并停止程式,否則,轉到 3
- 原始碼動態匯入安裝的模塊
- 代碼在需要時照常使用模塊
問題是代碼在 (4) 處使用tkfilebrowser模塊失敗。這是我的匯入代碼:
already_imported = {}
def do_import(dependency):
"""
Imports the dependency module
:param dependency: the name of the dependency module to import
:raise: ModuleNotFoundError if not found
:return: the imported module
"""
global already_imported
if dependency in already_imported:
result_module = already_imported[dependency]
else:
result_module = importlib.import_module(dependency)
already_imported[dependency] = result_module
return result_module
就在通話之后,這是背景關系:
>>> tkfilebrowser = do_import('tkfilebrowser')
>>> dir(tkfilebrowser)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
>>> type(tkfilebrowser)
<class 'module'>
>>> print(tkfilebrowser)
<module 'tkfilebrowser' (namespace)>
>>> tkfilebrowser.askopendirname()
AttributeError: module 'tkfilebrowser' has no attribute 'askopendirname'
我猜不出這里出了什么問題以及正確使用該模塊應該做些什么。
uj5u.com熱心網友回復:
一切都在源代碼中作業。
問題是我在 PyCharm 的運行配置中使用了不正確的 Python 二進制檔案,所以,當我意識到時,我改變了它,一切順利。
因此,如果有人遇到類似的錯誤,請確保您的 virtualenv 或您配置的任何 Python 環境確實正在被使用,而不是另一個。
在 PyCharm 中:
編輯配置 => Python 解釋器
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483739.html
