Re: find out whether a module exists (without importing it)

2012-08-07 Thread Ramchandra Apte
You are correct. On 7 August 2012 14:38, Chris Angelico wrote: > On Tue, Aug 7, 2012 at 6:00 PM, Gelonida N wrote: > > modulename = 'my.module' > > cmd = 'import %s as amodule' > > try: > > exec(cmd) > > print "imported successfully" > > Someone will doubtless correct me if I'm wrong, b

Re: find out whether a module exists (without importing it)

2012-08-07 Thread Chris Angelico
On Tue, Aug 7, 2012 at 6:00 PM, Gelonida N wrote: > modulename = 'my.module' > cmd = 'import %s as amodule' > try: > exec(cmd) > print "imported successfully" Someone will doubtless correct me if I'm wrong, but I think you can avoid exec here with: amodule=__import__(modulename) ChrisA

Re: find out whether a module exists (without importing it)

2012-08-07 Thread Peter Otten
Gelonida N wrote: > Is this possible. > > let's say I'd like to know whether I could import the module > 'mypackage.mymodule', meaning, > whther this module is located somewhere in sys.path > > i tried to use > > imp.find_module(), but > it didn't find any module name containing a '.' You coul

Re: find out whether a module exists (without importing it)

2012-08-07 Thread Gelonida N
Hi Michael, On 08/07/2012 08:43 AM, Michael Poeltl wrote: in my opinion, "without importing it" makes it unnecessarily complicated. It does, but I think this is what I want, thus my question. I tried to keep my question simple without explaining too much. Well now here's a little more context

Re: find out whether a module exists (without importing it)

2012-08-06 Thread Michael Poeltl
in my opinion, "without importing it" makes it unnecessarily complicated. You just want to know it module xyz exists, or better said can be found (sys.path). why not try - except[ - else ] try: import mymodule except ImportError: # NOW YOU KNOW it does not exist #+ and you may react

Re: find out whether a module exists (without importing it)

2012-08-06 Thread Gelonida N
On 08/06/2012 11:58 PM, Miki Tebeka wrote: imp.find_module(), but it didn't find any module name containing a '.' The docs (http://docs.python.org/library/imp.html#imp.find_module) clearly say: "This function does not handle hierarchical module names(names > containing dots). Thanks, Well this

Re: find out whether a module exists (without importing it)

2012-08-06 Thread Miki Tebeka
> imp.find_module(), but > it didn't find any module name containing a '.' The docs (http://docs.python.org/library/imp.html#imp.find_module) clearly say: "This function does not handle hierarchical module names (names containing dots). In order to find P.M, that is, submodule M of package P, use