By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,539 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,539 IT Pros & Developers. It's quick & easy.

look for a module without import it

P: n/a
Hi list,
I have to know if a module are present on the system, but I don't want
to import it. Only know if it is present.
I think that a loop on the site-packages directory can do the work, but
is there another solution?

Thanks,
Michele
Jul 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Michele Petrazzo wrote:
I have to know if a module are present on the system, but I don't want
to import it. Only know if it is present.

I think that a loop on the site-packages directory can do the work, but
is there another solution?
more module.py
print "I'M MODULE!"
python
>>import imp
imp.find_module("os")
(<open file 'C:\python25\lib\os.py', mode 'U' at 0x00A7E2F0>, 'C:\\python25\\lib\\os.py', ('.py',
'U', 1))
>>imp.find_module("sys")
(None, 'sys', ('', '', 6))
>>imp.find_module("foobar")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named foobar
>>imp.find_module("module")
(<open file 'module.py', mode 'U' at 0x00A7E2F0>, 'module.py', ('.py', 'U', 1))
>>import module
I'M MODULE!

</F>

Jul 14 '06 #2

P: n/a
Fredrik Lundh wrote:
>more module.py
print "I'M MODULE!"
>python
>>>import imp imp.find_module("os")
It was!

Michele
Jul 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.