opposite of import
9 answers - 78 bytes -

Hi
I am new to python. I wanted to know if there is an opposite of "import"
No.1 | | 225 bytes |
| 
pranav.choudhary (AT) gmail (DOT) com <pranav.choudhary (AT) gmail (DOT) comwrote:
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?
No.2 | | 1467 bytes |
| 
3 Aug 2006 04:50:35 -0700, pranav.choudhary (AT) gmail (DOT) com
<pranav.choudhary (AT) gmail (DOT) comwrote:
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. ;-)
No.3 | | 339 bytes |
| 
pranav.choudhary (AT) gmail (DOT) 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
No.4 | | 551 bytes |
| 
8/3/06, Simon Brunning <simon (AT) brunningonline (DOT) netwrote:
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.
No.5 | | 631 bytes |
| 
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
No.6 | | 541 bytes |
| 
8/3/06, Gerhard Fiedler <gelists (AT) gmail (DOT) comwrote:
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. 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) sysmodules And you
mess with *that* at your peril. ;-)
No.7 | | 948 bytes |
| 
Gerhard Fiedler wrote:
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).
No.8 | | 365 bytes |
| 
Simon Brunning wrote:
but for the fact that Python
stashes a reference to the module in (IIRC) sysmodules 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).
No.9 | | 327 bytes |
| 
8/3/06, John Salerno <johnjsal (AT) nospamgmail (DOT) comwrote:
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 sysmodules