。我想使用Python的imp的find_module
方法。但是,我不太明白为什么下面的方法不起作用。I'm in a directory which contains a module iclib
:
kurt@kurt-ThinkPad:~/dev/ipercron-compose/furion$ tree
.
├── iclib
│ ├── __init__.py
In that directory I can (in iPython) import
iclib
:In [1]: import iclib
I can also use
find_module
without a path
argument:In [1]: import imp
In [2]: imp.find_module('iclib')
Out[2]: (None, 'iclib', ('', '', 5))
但是,如果我试图仅在当前目录中使用
find_module
,则会出现错误:In [3]: import os
In [4]: imp.find_module('iclib', os.getcwd())
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-ada6f3744e78> in <module>()
----> 1 imp.find_module('iclib', os.getcwd())
ImportError: No frozen submodule named /home/kurt/dev/ipercron-compose/furion.iclib
为什么这不管用?
最佳答案
Following this issue on bugs.python.org, the path
argument needs to be embedded within a list:
In [4]: imp.find_module('iclib',[os.getcwd()])
Out[4]: (None, '/home/kurt/dev/ipercron-compose/furion/iclib', ('', '', 5))
如果
os.getcwd()
周围有方括号,函数将返回预期的输出。关于python - 在Python的imp中,“ImportError:没有名为…的卡住子模块”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41127133/