Import trouble

F

Frans Englich

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
 
S

Steve Holden

Frans said:
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)

You should post the exact error message together with a snippet of the
code around the error. One possibility is that the "shebang" line
(#!...) in your script is invoking a different Python than when you
enter "python" and the shell locates it on your path.
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?
Craig Ringer has already answered this question.
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?
Again you should post a little more detail. In your example it looks
like you made a simple spelling error: getDOMImpmentation instead of
getDOMImplementation.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top