473,790 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: A package import question

En Fri, 13 Jun 2008 20:01:56 -0300, Dan Yamins <dy*****@gmail. com>
escribió:
I'm having a problem importing a package in python, deleting some of
what's
been imported, and then reimporting. (I'm the sure the problem is
trivial,
but I just don't understand it.)

I have a directory of python modules called Operations. It contains a
python module called archive.py. Here's a import of the archive module
via package import:

Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright" , "credits" or "license" for more information.
>>import Operations.arch ive
>>Operations.ar chive
<module 'Operations.arc hive' from 'Operations/archive.pyc'>

So far, so good.
Note that if you execute dir() at this point, you'll see the Operations
name, *not* Operations.arch ive.
The statement "import Operations.arch ive" first tries to locate and load a
module named Operations - and *that* name is added to the current
namespace, not Operations.arch ive (which is an invalid name by itself).
But now, suppose I want to delete Operations.arch ive. then, I can't
reimport it. instead, I
>>del Operations.arch ive
You have removed the "archive" attribute from the object to which the
"Operations " name is referring to.
>>import Operations.arch ive
Python keeps a reference to all imported modules in sys.modules; if a
module was already imported, any subsequent imports of the same module
just return the existing reference.
If you want to force Python to re-read the module from file, use the
reload function. But please read the warnings at
http://docs.python.org/lib/built-in-funcs.html#l2h-61
>>dir()
['Operations', '__builtins__', '__doc__', '__name__']
>>>

Instead of getting 'Operations.arc hive', I just seem to get 'Operations'.
You would never get a dotted name from dir(), unless you play tricks with
locals()/globals()
I can't seem to be able to import Operations.arch ive without quitting the
python interpreter and starting again.

What's going on here, and how do I fix it?
reload() may be what you need, but again, make sure you read the
documentation before using it. reload is not a magic wand. Remember that
names imported from the old module definition continue to be bound to the
old objects, and all instances of classes defined in the old module
continue to use the old class definitions, among other things.

--
Gabriel Genellina

Jun 27 '08 #1
0 1111

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

Similar topics

2
1654
by: Mathias Waack | last post by:
Hi, I've got a problem with Fredrik's squeeze tool. I've got a script which accesses a Oracle DB with the help of DCOracle. This script works very well, but after squeezing it the "import DCOracle" fails. The DCOracle package consists of several python files and some shared libs. The import of one shared lib from within the package fails. It looks like a problem with the code responsible for searching the appropriate module file.
4
4202
by: Robert Ferrell | last post by:
I have a question about how to organize modules in a package. I think there is something fundamental that I am missing. Say I'm creating a package, GreatPackage, which has three sub-packages, myUtilities, GoodStuff, MoreGoodStuff. The directories look like: GreatPackage |
0
1230
by: David M. Wilson | last post by:
Hello! I maintain a small package for talking to the API of BulkSMS.co.uk. I have been adding support for some new features recently, and found myself slightly indecisive over how best to lay out the modules internally. I have a 'BulkSMS.Exceptions' module, which contains a base class and (currently) all derived exceptions that my package throws. This has been fine up until now, as the exceptions were generated by only one
3
3820
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
5
3135
by: selimsivad83 | last post by:
Hi everyone, Thinking in Java, 4th edition, p 232: ... if you don't put an access specifier for class access, it defaults to package access. ... However, if a static member of that class is public, the client programmer can still access that static member even though they cannot create an object of that class. However the code below produces an error:...
4
6504
by: siggi | last post by:
Hi all, a newbie question: I have a program gui03A.py using wxPython, importing it such: "from wxPython.wx import *" The program works, but I get the warning message: "gui03A.py:4: DeprecationWarning: The wxPython compatibility package is no
0
862
by: Tim Cook | last post by:
Hi All, I would like feedback on the proper/best 'Pythonic' approach. This is a rather subjective question. Where is the trade-off between package name lengths and faithfulness to the specifications? I am implementing a set of specifications for healthcare IT for Python
0
1069
by: Roger Ineichen | last post by:
Hi Tim For a usecase like this, I personaly recommend to defina all interfaces in one module which probably is a namespace if you need alot of interfaces to define. e.g. openehr.interfaces.foobar.IFooBar
7
2196
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try it complains that I am attempting a relative import in a non-package. eg ~/python/testpackage$ ls config.py importer.py __init__.py
0
9666
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
10200
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
9986
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
9021
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
7529
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
6769
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
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
3703
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.