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

2012-08-07 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 properly
??
* Gelonida N gelon...@gmail.com [2012-08-06 22:49]:
 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 '.'
 
 Am I doing anything wrong?
 
 Is there another existing implementation, that helps.
 
 I could do this manually, but this is something I'd just like to do
 if necessary.
 
 
 -- 
 http://mail.python.org/mailman/listinfo/python-list

-- 
Michael Poeltl
Computational Materials Physics  voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12 fax:   +43-1-4277-9514 (or 9513) 
A-1090 Wien, AUSTRIA   cmp.mpi.univie.ac.at 
---
ubuntu-11.10 | vim-7.3 | python-3.2.2 | mutt-1.5.21 | elinks-0.12
---
-- 
http://mail.python.org/mailman/listinfo/python-list


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.


There's two reasons why I sepcified the without importing it.
Some modules may have side effects when being imported,and sometimes I 
just want to check for a module's existence



Second:
Sometimes I only want to know, whether a module exists.
I do not want to know whether a module is syntactically correct or 
whether a module if imported  is capable of

importing all it's submodules

What I'd like to achieve at the moment is to distinguish three situations:
- a module with a given name does not exist
- a module with a given name exists and produces errors (might be 
ImportErors)

- a module with a given name exists and can be imported

In fact what I really want to achieve is:
import a module if it exists (and fail if it is broken)
if it doesn't exist import a 'default' module and go on.

The name of the module is stored in a variable and is not known prior to 
running the script so the code, that you suggested would be something like.



modulename = 'my.module'
cmd = 'import %s as amodule'
try:
exec(cmd)
print imported successfully
except ImportError:
   print module doesn't exist or the module tries to  \
import another module that doesn't exist
   # if the module doesn't exist I'd like to import a 'fallback' module
   # otherwise I'd like to abort.
except Exception as exc:
print module exists, but is broken
raise exc

amodule.do_something()



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 properly
??
* Gelonida N gelon...@gmail.com [2012-08-06 22:49]:

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 '.'

Am I doing anything wrong?

Is there another existing implementation, that helps.

I could do this manually, but this is something I'd just like to do
if necessary.


--
http://mail.python.org/mailman/listinfo/python-list


--
http://mail.python.org/mailman/listinfo/python-list


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 could look for the toplevel name and then look for modules in the 
corresponding directory. This is not always reliable as a package may not 
correspond to a directory (e. g.: os and os.path), but I don't think you can 
get any closer without performing an actual import.

-- 
http://mail.python.org/mailman/listinfo/python-list


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 gelon...@gmail.com 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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 ros...@gmail.com wrote:

 On Tue, Aug 7, 2012 at 6:00 PM, Gelonida N gelon...@gmail.com 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
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


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 
find_module() and load_module() to find and load package P, and then use 
find_module() with the path argument set to P.__path__. When P itself has a 
dotted name, apply this recipe recursively.

See https://gist.github.com/3278829 for possible implementation.
-- 
http://mail.python.org/mailman/listinfo/python-list


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 explains.

  In order to find P.M, that is, submodule M of package P, use 
find_module() and load_module() to find and load package P, and then use 
find_module() with the path argument set to P.__path__. When P itself 
has a dotted name, apply this recipe recursively.


See https://gist.github.com/3278829 for possible implementation.




Using imp and then iterating (as you suggested) is probably the fastest 
solution. This is what I will do.


Thanks again.

--
http://mail.python.org/mailman/listinfo/python-list