473,701 Members | 2,628 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 1621
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
4490
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
1931
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
3812
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
2377
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
5018
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
3666
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
20557
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
3927
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
1406
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
1501
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
8736
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
9229
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9083
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...
0
8934
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7824
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...
0
5904
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
4410
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3102
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
3
2035
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.