Import trouble

Frans Englich frans.englich at telia.com
Wed Dec 15 08:45:13 EST 2004


Hello all,

I have a couple of questions related to module importing.

1) When I start my Python program with `python foo.py` instead of simply 
adding a interpreter comment on the first line and do `./foo.py`, some 
"local" imports fails, I guess because current working directory is 
different. Is CWD the only difference between running `python foo.py` and 
`./foo.py`? What is the recommended way? (I guess security is one aspect that 
can be argued)

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?

3) And the last -- but funniest -- import problem: sometimes imports of 
modules in "xml." fails, sometimes it works. For example, this code:
from xml.dom.minidom import getDOMImplementation
impl = getDOMImpmentation() #NameError: name 'getDOMImpmentation' is not 
defined

fails as shown, and other times it works. When running pydoc as local web 
server, it also sometimes fails, although I haven't been able to reproduce 
for a error message. I have recent versions of PyXML and 4suite installed. 

What is wrong? Any idea what that could be wrong with my setup, which could 
cause this?


Cheers,

	Frans



More information about the Python-list mailing list