472,122 Members | 1,432 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 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 1010

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mathias Waack | last post: by
4 posts views Thread by Robert Ferrell | last post: by
reply views Thread by David M. Wilson | last post: by
4 posts views Thread by siggi | last post: by
reply views Thread by Tim Cook | last post: by
reply views Thread by Roger Ineichen | last post: by
reply views Thread by leo001 | last post: by

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.