escribió:
I'm having a problem importing a package in python, deleting some ofNote that if you execute dir() at this point, you'll see the Operations
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.<module 'Operations.archive' from 'Operations/archive.pyc'>>>import Operations.archive
>>Operations.archive
So far, so good.
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'tYou have removed the "archive" attribute from the object to which the
reimport it. instead, I
>>del Operations.archive
"Operations" name is referring to.
Python keeps a reference to all imported modules in sys.modules; if a>>import Operations.archive
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
You would never get a dotted name from dir(), unless you play tricks with['Operations', '__builtins__', '__doc__', '__name__']>>dir()>>>
Instead of getting 'Operations.archive', I just seem to get 'Operations'.
locals()/globals()
I can't seem to be able to import Operations.archive without quitting thereload() may be what you need, but again, make sure you read the
python interpreter and starting again.
What's going on here, and how do I fix it?
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