look for a module without import it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michele Petrazzo

    #1

    look for a module without import it

    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
  • Fredrik Lundh

    #2
    Re: look for a module without import it

    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_modu le("os")
    (<open file 'C:\python25\li b\os.py', mode 'U' at 0x00A7E2F0>, 'C:\\python25\\ lib\\os.py', ('.py',
    'U', 1))
    >>imp.find_modu le("sys")
    (None, 'sys', ('', '', 6))
    >>imp.find_modu le("foobar")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named foobar
    >>imp.find_modu le("module")
    (<open file 'module.py', mode 'U' at 0x00A7E2F0>, 'module.py', ('.py', 'U', 1))
    >>import module
    I'M MODULE!

    </F>



    Comment

    • Michele Petrazzo

      #3
      Re: look for a module without import it

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

      Michele

      Comment

      Working...