Proposed Python api for testing whether a module exists

I propose to add the following function to the imp module: exists(module_name) Test whether the named module can be found. This method should be used when you want to perform alternate actions depending on whether a module is available. Returns True of the module can be found and False otherwise. A True return value does not guarentee that the module will be importable. The module will not be imported as a result of calling exists, but parent modules may be. If a dotted module name is given, then parent modules will be imported if they haven't already been imported. If there are no objections, I'll try to get this in before Wednesday night. Jim -- Jim Fulton mailto:jim@zope.com Python Powered! CTO (540) 361-1714 http://www.python.org Zope Corporation http://www.zope.com http://www.zope.org

At 12:32 PM 8/2/04 -0400, Jim Fulton wrote:
I propose to add the following function to the imp module:
exists(module_name)
Test whether the named module can be found.
This method should be used when you want to perform alternate actions depending on whether a module is available.
Returns True of the module can be found and False otherwise. A True return value does not guarentee that the module will be importable. The module will not be imported as a result of calling exists, but parent modules may be.
If a dotted module name is given, then parent modules will be imported if they haven't already been imported.
If there are no objections, I'll try to get this in before Wednesday night.
Is this intended to work in the presence of import hooks? In particular, all the hooks offered by PEP 302, but also systems like 'iu', 'imputil', etc. Or, more specifically, will this be implemented in terms of the PEP 302 importer/loader protocols, or will those protocols need extending in order to allow such loaders (e.g. zipimport) to work with this new function?

Phillip J. Eby wrote:
At 12:32 PM 8/2/04 -0400, Jim Fulton wrote:
I propose to add the following function to the imp module:
...
Is this intended to work in the presence of import hooks? In particular, all the hooks offered by PEP 302, but also systems like 'iu', 'imputil', etc.
Or, more specifically, will this be implemented in terms of the PEP 302 importer/loader protocols, or will those protocols need extending in order to allow such loaders (e.g. zipimport) to work with this new function?
Paul Moore wrote: ...
Does this work in the presence of PEP 302 style import hooks? Specifically, the case where there's a zipfile on sys.path.
Good question. I was planning to implement this in terms of imp.find_module, but that won't work in light of PEP 302. Dang. It's too bad that the imp.get_loader proposed in PEP 302 isn't implemented. Paul, do you have an implementation of what would have been imp.get_loader? Jim -- Jim Fulton mailto:jim@zope.com Python Powered! CTO (540) 361-1714 http://www.python.org Zope Corporation http://www.zope.com http://www.zope.org

Jim Fulton <jim@zope.com> writes:
Phillip J. Eby wrote:
At 12:32 PM 8/2/04 -0400, Jim Fulton wrote:
I propose to add the following function to the imp module:
...
Is this intended to work in the presence of import hooks? In particular, all the hooks offered by PEP 302, but also systems like 'iu', 'imputil', etc. Or, more specifically, will this be implemented in terms of the PEP 302 importer/loader protocols, or will those protocols need extending in order to allow such loaders (e.g. zipimport) to work with this new function?
Paul Moore wrote: ...
Does this work in the presence of PEP 302 style import hooks? Specifically, the case where there's a zipfile on sys.path.
Good question. I was planning to implement this in terms of imp.find_module, but that won't work in light of PEP 302. Dang. It's too bad that the imp.get_loader proposed in PEP 302 isn't implemented.
Paul, do you have an implementation of what would have been imp.get_loader?
Sorry, no. Just did the implementation, I mainly did some of the write-up, testing, and cheerleading. Paul. -- Ooh, how Gothic. Barring the milk.

Jim Fulton <jim@zope.com> writes:
I propose to add the following function to the imp module:
exists(module_name) [...] If there are no objections, I'll try to get this in before Wednesday night.
Does this work in the presence of PEP 302 style import hooks? Specifically, the case where there's a zipfile on sys.path. Paul. -- A little inaccuracy sometimes saves tons of explanation -- Saki

[Jim Fulton]
I propose to add the following function to the imp module:
exists(module_name)
Test whether the named module can be found.
+1. This can be much better than catching ImportError, for reasons previously given. site.py should be changed to use this to determine whether sitecustomize exists (as is, if sitecustomize exists but screws up by trying to import something that isn't on sys.path, site.py suppresses the ImportError raised *by* sitecustomize; this is bad).

On Mon, 2004-08-02 at 12:32, Jim Fulton wrote:
I propose to add the following function to the imp module:
exists(module_name)
Test whether the named module can be found.
This method should be used when you want to perform alternate actions depending on whether a module is available.
Returns True of the module can be found and False otherwise. A True return value does not guarentee that the module will be importable. The module will not be imported as a result of calling exists, but parent modules may be.
If a dotted module name is given, then parent modules will be imported if they haven't already been imported.
+1, but shouldn't "parent module" really be spelled "parent package"? -Barry
participants (5)
-
Barry Warsaw
-
Jim Fulton
-
Paul Moore
-
Phillip J. Eby
-
Tim Peters