我有一個腳本,執行以下操作:導入模塊小鬼
import imp
imp.load_source("storage_configuration_reader","/bi/opt/RNAspace/rnaspace_sources/rnaspace/rnaspace/rnaspace/dao/storage_configuration_reader.py")
後來,我調用類的這個模塊的名稱相同:
config = storage_configuration_reader()
如果我進口它像上面我得到以下NameError NameError: global name 'storage_configuration_reader' is not defined
但如果我使用下面的代碼:
import imp
imp.load_source("storage_configuration_reader","/bi/opt/RNAspace/rnaspace_sources/rnaspace/rnaspace/rnaspace/dao/storage_configuration_reader.py")
import storage_configuration_reader
config = storage_configuration_reader()
然後我得到的第被錯誤TypeError: 'module' object is not callable
改變imp.load_source
的名稱不利於導入對象:
import imp
imp.load_source("storage_configuration","/bi/opt/RNAspace/rnaspace_sources/rnaspace/rnaspace/rnaspace/dao/storage_configuration_reader.py")
<module 'storage_configuration' from '/bi/opt/RNAspace/rnaspace_sources/rnaspace/rnaspace/rnaspace/dao/storage_configuration_reader.pyc'>
import storage_configuration
config = storage_configuration_reader()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'storage_configuration_reader' is not defined
哪個是進口這樣一個對象的最佳方式(AKA工作方式)?
信息:該storage_configuration_reader定義:
模塊確實不可調用。你爲什麼要打電話?也許你希望在模塊中調用該類而不是? – Bach
是的,這就是我想要的。 – Llopis