A package import question

Dan Yamins dyamins at gmail.com
Sat Jun 14 03:38:24 CEST 2008


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
>
> Gabriel, thanks.  I understood about the fact that import only loads the
first time, but didn't realize that "del" only removes the bound reference
to the object, not as I had hoped the thing from the namespace itself.

Also, I did _try_ to use reload.  however, that failed since .archive was no
longer an attribute associated with Operations:

 >>> import Operations.archive
 >>> del Operations.archive
 >>> reload(Operations.archive)
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 AttributeError: 'module' object has no attribute 'archive'


It seems unfortunately that the "del" operation on the one hand doesn't
really remove the .archive from memory but just it's bound name, but
nonetheless prevents me from reloading the object kept in memory.

I guess I'm trying to find a clean way to remove all the attributes
associated with a module when I reload it.   Is there any way to do this?
(The operation of recursively searching through the attributes of the module
and deleting those first seems to be bad since when I did that and then try
to _reload_ the module, the attributes I deleted are _not_ reloaded.)




On Fri, Jun 13, 2008 at 9:09 PM, Gabriel Genellina <gagsl-py2 at yahoo.com.ar>
wrote:

> En Fri, 13 Jun 2008 20:01:56 -0300, Dan Yamins <dyamins at 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080613/5acbb7f9/attachment.html>


More information about the Python-list mailing list