En Fri, 13 Jun 2008 20:01:56 -0300, Dan Yamins <[EMAIL PROTECTED]> 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

Reply via email to