473,405 Members | 2,415 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,405 software developers and data experts.

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.archive
>>Operations.archive
<module 'Operations.archive' 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.archive.
The statement "import Operations.archive" first tries to locate and load a
module named Operations - and *that* name is added to the current
namespace, not Operations.archive (which is an invalid name by itself).
But now, suppose I want to delete Operations.archive. then, I can't
reimport it. instead, I
>>del Operations.archive
You have removed the "archive" attribute from the object to which the
"Operations" name is referring to.
>>import Operations.archive
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.archive', 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.archive 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 1085

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

Similar topics

2
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...
4
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,...
0
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...
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...
5
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...
4
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:...
0
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...
0
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. ...
7
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...

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.