Reputation: 57391
I'm trying to write a script which searches a directory for a module with a given name. I'd like to use the find_module
method of Python's imp. However, I don't quite understand why the following doesn't work. 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))
However, if I try to use find_module
in the current directory only, I get an error:
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
Why doesn't this work?
Upvotes: 2
Views: 1416
Reputation: 57391
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))
With square brackets around the os.getcwd()
, the function returns the expected output.
Upvotes: 9