opposite of import

  • Thread starter pranav.choudhary
  • Start date
P

pranav.choudhary

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

Claudio Grondi

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
 
S

Simon Brunning

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. ;-)
 
S

Simon Brunning

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.
 
G

Gerhard Fiedler

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
 
S

Simon Brunning

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. ;-)
 
D

Duncan Booth

Gerhard said:
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).
 
J

John Salerno

Simon said:
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).
 
S

Simon Brunning

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__.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top