Import trouble
Doran_Dermot at emc.com
Doran_Dermot at emc.com
Wed Dec 15 08:54:33 EST 2004
Hi Craig,
How about creating your own module that does this in __init__.py.
You could create a directory (Eg craig_init) and in that directory create
the file __init__.py containing the following code:
try: import libxml
except:
# Blah, blah, blah. A clever bit of code goes here!
WARNING: I haven't checked this myself, but it appears to be the way that
the Zope Database python code has been written. If you have the ZODB
package installed take a look at "Lib\site-packages\Btrees\__init__.py".
I'm sure there must be other examples of this as well.
Cheers!!
-----Original Message-----
From: python-list-bounces+doran_dermot=emc.com at python.org
[mailto:python-list-bounces+doran_dermot=emc.com at python.org] On Behalf Of
Craig Ringer
Sent: 15 December 2004 13:45
To: Frans Englich
Cc: Python mailing list
Subject: Re: Import trouble
On Wed, 2004-12-15 at 21:45, Frans Englich wrote:
> 2) I use Python modules which are not usually installed(libxml2/libxslt)
and
> want to fail gracefully in case the modules aren't available; print an
> informative message. Since these imports are done in several related
> programs, I don't want to duplicate the code, but instead centralize it.
The
> problem is that when I put the module imports/exception code in a
function,
> it is as if it wasn't imported, even though there was no exceptions. I
> suspect this is because the import is only done into the current
namespace:
> the function scope(instead of file scope as I want). Is there any solution
to
> my problem? Or should I solve it in another way?
def import_xml:
try:
import libxml
except ImportError,err:
# handle the error
return libxml
libxml = import_xml()
--
Craig Ringer
--
http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list