FAQ
Hi
I am new to python. I wanted to know if there is an opposite of "import"

Search Discussions

  • Lawrence Oluyede at Aug 3, 2006 at 11:54 am

    pranav.choudhary at gmail.com wrote:

    I am new to python. I wanted to know if there is an opposite of "import"
    What do you mean? What are you trying to accomplish?

    --
    Lawrence - http://www.oluyede.org/blog
    "Nothing is more dangerous than an idea
    if it's the only one you have" - E. A. Chartier
  • Claudio Grondi at Aug 3, 2006 at 12:16 pm

    pranav.choudhary at gmail.com wrote:
    Hi
    I am new to python. I wanted to know if there is an opposite of "import"
    If you mean 'import' adds something, so you ask how to get rid of
    something? Here you are:

    Look at the 'del' statement if it is what you are looking for.

    Claudio Grondi
  • Simon Brunning at Aug 3, 2006 at 12:17 pm

    On 3 Aug 2006 04:50:35 -0700, pranav.choudhary at gmail.com wrote:
    Hi
    I am new to python. I wanted to know if there is an opposite of "import"
    Err, depends upon what you mean by opposite.


    If you want to remove the module from a namespace into which you
    imported it, you can do that with del:

    import amodule
    amodule.afunction() # Works fine

    del amodule
    amodule.afunction() # Will die now


    If you want to "de-import" a module in order that you can import a
    new, updated version, you can do that (with caveats) with the reload
    built-in function. Check out the docs.


    If you want a module to be able to specify what happens when it is
    imported elsewhere, well, you have some measure of control there, too.
    All the top level code will be executed (other than that in a "if
    n__name == '__main__'" block).

    If the import is of the "import spam" form, all names in the imported
    namespace will be available in the importing module.

    If the import os of the "from spam import *" form, only *public* names
    in the module namespace are made available to the importing module.
    Public names are those defined in a top level __all__ list, or not
    starting with an underscore of no such list has been defined.


    And if none of those are what you meant by the opposite of an import,
    you'll need to be more explicit. ;-)

    --
    Cheers,
    Simon B,
    simon at brunningonline.net,
    http://www.brunningonline.net/simon/blog/
  • Simon Brunning at Aug 3, 2006 at 12:26 pm

    On 8/3/06, Simon Brunning wrote:
    If you want to remove the module from a namespace into which you
    imported it, you can do that with del:

    import amodule
    amodule.afunction() # Works fine

    del amodule
    amodule.afunction() # Will die now
    Note that this doesn't get rid of a module entirely. Python will still
    holds on to the module, and if you just import it again at this point,
    it won't be re-executed - you'll just get another reference to the
    original module.

    --
    Cheers,
    Simon B,
    simon at brunningonline.net,
    http://www.brunningonline.net/simon/blog/
  • Gerhard Fiedler at Aug 3, 2006 at 1:37 pm

    On 2006-08-03 09:26:54, Simon Brunning wrote:

    import amodule
    amodule.afunction() # Works fine

    del amodule
    amodule.afunction() # Will die now
    Note that this doesn't get rid of a module entirely. Python will still
    holds on to the module, and if you just import it again at this point,
    it won't be re-executed - you'll just get another reference to the
    original module.
    Is that guaranteed, or is that just until the garbage collector has removed
    the module (at some arbitrary point)?

    Gerhard
  • Simon Brunning at Aug 3, 2006 at 1:46 pm

    On 8/3/06, Gerhard Fiedler wrote:
    Is that guaranteed, or is that just until the garbage collector has removed
    the module (at some arbitrary point)?
    I *think* it's guaranteed.

    It's not a matter for the garbage collector. GC only exists to remove
    cyclic references. Ordinary reference counting *would* remove the
    module as soon as you de referenced it, but for the fact that Python
    stashes a reference to the module in (IIRC) sys.__modules__. And you
    mess with *that* at your peril. ;-)

    --
    Cheers,
    Simon B,
    simon at brunningonline.net,
    http://www.brunningonline.net/simon/blog/
  • John Salerno at Aug 3, 2006 at 2:05 pm

    Simon Brunning wrote:

    but for the fact that Python
    stashes a reference to the module in (IIRC) sys.__modules__. And you
    mess with *that* at your peril. ;-)
    According to Python in a Nutshell, references are stored in the
    dictionary sys.modules, but I'm not sure if it matters that it's not
    __modules__ instead (unless that also exists).
  • Simon Brunning at Aug 3, 2006 at 2:28 pm

    On 8/3/06, John Salerno wrote:
    According to Python in a Nutshell, references are stored in the
    dictionary sys.modules, but I'm not sure if it matters that it's not
    __modules__ instead (unless that also exists).
    Right you are - it's sys.modules, not sys.__modules__.

    --
    Cheers,
    Simon B,
    simon at brunningonline.net,
    http://www.brunningonline.net/simon/blog/
  • Duncan Booth at Aug 3, 2006 at 2:02 pm

    Gerhard Fiedler wrote:
    On 2006-08-03 09:26:54, Simon Brunning wrote:

    import amodule
    amodule.afunction() # Works fine

    del amodule
    amodule.afunction() # Will die now
    Note that this doesn't get rid of a module entirely. Python will
    still holds on to the module, and if you just import it again at this
    point, it won't be re-executed - you'll just get another reference to
    the original module.
    Is that guaranteed, or is that just until the garbage collector has
    removed the module (at some arbitrary point)?
    It is guaranteed that simply deleting the module from the module which
    imported it won't be sufficient to throw it away as other references to the
    module will remain (in particular the list of modules used by the import
    mechanism to ensure you get the same module if you re-import it again in
    the future).

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 3, '06 at 11:50a
activeAug 3, '06 at 2:28p
posts10
users7
websitepython.org

People

Translate

site design / logo © 2021 Grokbase