473,666 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(libxm l2/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 getDOMImplement ation
impl = getDOMImpmentat ion() #NameError: name 'getDOMImpmenta tion' 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 1620
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(libxm l2/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 getDOMImplement ation
impl = getDOMImpmentat ion() #NameError: name 'getDOMImpmenta tion' 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: getDOMImpmentat ion instead of
getDOMImplement ation.

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
4489
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
1930
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, mainWindow() etc. and then each file will include a GUI class (which just happen to be a wxNotebook tab each). I'm having trouble accessing the imported classes though. For example I have login.py which contains something like this:
3
3810
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 'import os\ndef test():\n print os.getcwd()' > os.py cd /tmp
1
2376
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 rest of my application (read this as for any imported module) if I import in in a 'higher-level' module. For example: A.py ====
9
5012
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 and in module file2.py there exist command import file1? This is not working in C#. pujo
3
3664
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 of data are imported. I tried to remove 20 rows in the excel spreadsheet in the interval row 0-50. When i later ran the import, only 30 rows were imported. I deleted almost every row in the interval 0-50, with the result of the import having 0...
7
20552
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 _Ron
49
3913
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 isomorphism between filesystem and namespace, doesn't seem very well suited for big projects. However, I might not really understand the Pythonic way. I'm not sure if I have a specific question here, just a general plea for advice. 1) Namespace....
4
1404
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 Common/ __init__.py # does "from MyClass1 import MyClass1", etc,... MyClass1.py MyClass1a.py # depends on MyClass1
0
1491
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 me. Yes.
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.