Frans Englich wrote:

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
--
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to