473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Import trouble


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
Jul 18 '05 #1
1 1609
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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dag4004 | last post by:
Hello, I have a strange behavior with global and module. Here is the module named: foo.py bar = "egg"
5
by: simo | last post by:
I've written a pretty big wxPython script, and I thought I'd split the source into a few files. I'm going to have a main.py file which includes global defs, wxApp initialisation code,...
3
by: George P | last post by:
I've run into a strange package related namespace problem. Follow these steps (on UNIX system) to illustrate: ------------------------- mkdir /tmp/mypkg cd /tmp/mypkg touch __init__.py echo...
1
by: Andrew James | last post by:
All, I'm having some trouble with understanding python's importing behaviour in my application. I'm using psyco to optimise part of my code, but I'm not sure whether it inherits throughout the...
9
by: ajikoe | last post by:
Hello, I have two modules (file1.py and file2.py) Is that ok in python (without any weird implication) if my module import each other. I mean in module file1.py there exist command import file2...
3
by: Elaksomfan | last post by:
Hi, I am having trouble importing data from an excel spreadsheet into MS SQL Server 2000 using DTS Wizard. The DTS import process is successfull, no errors, but only 50 rows of approx. 1500 rows...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
4
by: Brian Blais | last post by:
Hello, I am trying to organize some of my code, and am having a little trouble with the import logic. I find I often have something like: MyPackage/ Part1/ # wants to use functions in...
0
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.