Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2203 > unrolled thread

delete namespaces

Started bymonkeys paw <monkey@joemoney.net>
First post2011-03-29 21:14 -0400
Last post2011-03-30 22:11 -0700
Articles 11 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  delete namespaces monkeys paw <monkey@joemoney.net> - 2011-03-29 21:14 -0400
    Re: delete namespaces Raymond Hettinger <python@rcn.com> - 2011-03-29 18:32 -0700
    Re: delete namespaces Tim Chase <python.list@tim.thechases.com> - 2011-03-29 20:41 -0500
    Re: delete namespaces Terry Reedy <tjreedy@udel.edu> - 2011-03-30 00:57 -0400
      Re: delete namespaces Raymond Hettinger <python@rcn.com> - 2011-03-30 17:11 -0700
        Re: delete namespaces Chris Angelico <rosuav@gmail.com> - 2011-03-31 14:06 +1100
    Re: delete namespaces Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-03-30 11:10 +0200
    Re: delete namespaces Terry Reedy <tjreedy@udel.edu> - 2011-03-30 15:03 -0400
    Re: delete namespaces Tim Golden <mail@timgolden.me.uk> - 2011-03-30 20:08 +0100
    Re: delete namespaces Ritesh Nadhani <riteshn@gmail.com> - 2011-03-30 21:22 -0700
    Re: delete namespaces rusi <rustompmody@gmail.com> - 2011-03-30 22:11 -0700

#2203 — delete namespaces

Frommonkeys paw <monkey@joemoney.net>
Date2011-03-29 21:14 -0400
Subjectdelete namespaces
Message-ID<UtydnQbv7-6IGQ_QnZ2dnUVZ_hWdnZ2d@insightbb.com>
How do i delete a module namespace once it has been imported?

I use

import banner

Then i make a modification to banner.py. When i import it again,
the new changes are not reflected. Is there a global variable i can
modify?

[toc] | [next] | [standalone]


#2204

FromRaymond Hettinger <python@rcn.com>
Date2011-03-29 18:32 -0700
Message-ID<1588fe22-0f97-4b95-9406-5c517b9365f5@i4g2000pro.googlegroups.com>
In reply to#2203
On Mar 29, 6:14 pm, monkeys paw <mon...@joemoney.net> wrote:
> How do i delete a module namespace once it has been imported?
 . . .
> Then i make a modification to banner.py. When i import it again,
> the new changes are not reflected. Is there a global variable i can
> modify?

In Python2.x, you can use the reload() function:

Help on built-in function reload in module __builtin__:

reload(...)
    reload(module) -> module

    Reload the module.  The module must have been successfully
imported before.


Raymond

[toc] | [prev] | [next] | [standalone]


#2205

FromTim Chase <python.list@tim.thechases.com>
Date2011-03-29 20:41 -0500
Message-ID<mailman.4.1301449303.2990.python-list@python.org>
In reply to#2203
On 03/29/2011 08:14 PM, monkeys paw wrote:
> How do i delete a module namespace once it has been imported?
>
> I use
>
> import banner
>
> Then i make a modification to banner.py. When i import it again,
> the new changes are not reflected. Is there a global variable i can
> modify?

Delete it from sys.modules:

 >>> file('foo.py', 'w').write('x = 42\n')
 >>> import foo
 >>> foo.x
42
 >>> del foo
 >>> import sys
 >>> del sys.modules['foo']
 >>> file('foo.py', 'w').write('x = 999\n')
 >>> import foo
 >>> foo.x
999

Beware that if you still have old references to the module, they 
don't get refreshed.

-tkc

[toc] | [prev] | [next] | [standalone]


#2209

FromTerry Reedy <tjreedy@udel.edu>
Date2011-03-30 00:57 -0400
Message-ID<mailman.7.1301461050.2990.python-list@python.org>
In reply to#2203
On 3/29/2011 9:14 PM, monkeys paw wrote:
> How do i delete a module namespace once it has been imported?
>
> I use
>
> import banner
>
> Then i make a modification to banner.py. When i import it again,
> the new changes are not reflected.

The best thing, if possible, is to restart the program.
If you develop banner.py with adequate tests, you will want to restart 
the test anyway, and you should not need to modify much thereafter.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#2255

FromRaymond Hettinger <python@rcn.com>
Date2011-03-30 17:11 -0700
Message-ID<8ee61e93-6260-43dc-bc3c-aaadecc1fdb0@f31g2000pri.googlegroups.com>
In reply to#2209
[monkeys paw]
> > How do i delete a module namespace once it has been imported?
 . . .
> > Then i make a modification to banner.py. When i import it again,
> > the new changes are not reflected.

[Terry Reedy]
> The best thing, if possible, is to restart the program.
> If you develop banner.py with adequate tests, you will want to restart
> the test anyway, and you should not need to modify much thereafter.

This is excellent advice.

You're much better-off starting fresh each time.


Raymond

[toc] | [prev] | [next] | [standalone]


#2270

FromChris Angelico <rosuav@gmail.com>
Date2011-03-31 14:06 +1100
Message-ID<mailman.25.1301540817.2990.python-list@python.org>
In reply to#2255
On Thu, Mar 31, 2011 at 11:11 AM, Raymond Hettinger <python@rcn.com> wrote:
> [monkeys paw]
>> > How do i delete a module namespace once it has been imported?
>  . . .
>> > Then i make a modification to banner.py. When i import it again,
>> > the new changes are not reflected.
>
> [Terry Reedy]
>> The best thing, if possible, is to restart the program.
>> If you develop banner.py with adequate tests, you will want to restart
>> the test anyway, and you should not need to modify much thereafter.
>
> This is excellent advice.
>
> You're much better-off starting fresh each time.

Each language should be used for its strengths, not its weaknesses :)
If you're using Python, keep it light and simple and then just restart
the program. If you want to reload stuff without restarting, grab
Pike. There's no point fighting your language!

ChrisA

[toc] | [prev] | [next] | [standalone]


#2218

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2011-03-30 11:10 +0200
Message-ID<mailman.10.1301476243.2990.python-list@python.org>
In reply to#2203
monkeys paw wrote:
> How do i delete a module namespace once it has been imported?
>
> I use
>
> import banner
>
> Then i make a modification to banner.py. When i import it again,
> the new changes are not reflected. Is there a global variable i can
> modify?
It depends on what you want to achieve.

1/ if you want to re-import your module because it contains User data 
that may have been updated, one way is to make sure all you definitions 
are at the module level and use the execfile statement. ofc the file is 
executed, so it can be done only in a trusted environment.

2/ if you want to reload your module because you changed the code and 
want to test it, the best way to do it is to write a test file that will 
do all the tests so that restarting the test is cheap. Testing from a 
newly created python process is always the best solution, if available.

3/ if you want to do the 2/ but require a painful long prologue to your 
test, then you may want to use the builtin reload. Use it with care, 
because any existing object created from the previous module will not be 
affected, they'll still hold the previous code. "reload" solves some 
problems, but bring others, especially for the newcomer.


JM

[toc] | [prev] | [next] | [standalone]


#2242

FromTerry Reedy <tjreedy@udel.edu>
Date2011-03-30 15:03 -0400
Message-ID<mailman.18.1301511798.2990.python-list@python.org>
In reply to#2203
On 3/30/2011 5:10 AM, Jean-Michel Pichavant wrote:

> 3/ if you want to do the 2/ but require a painful long prologue to your
> test, then you may want to use the builtin reload. Use it with care,
> because any existing object created from the previous module will not be
> affected, they'll still hold the previous code. "reload" solves some
> problems, but bring others, especially for the newcomer.

Guido removed it in 3.x because it is badly flawed and he could see any 
way to sensibly fix it.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#2243

FromTim Golden <mail@timgolden.me.uk>
Date2011-03-30 20:08 +0100
Message-ID<mailman.19.1301512139.2990.python-list@python.org>
In reply to#2203
On 30/03/2011 8:03 PM, Terry Reedy wrote:
> On 3/30/2011 5:10 AM, Jean-Michel Pichavant wrote:
>
>> 3/ if you want to do the 2/ but require a painful long prologue to your
>> test, then you may want to use the builtin reload. Use it with care,
>> because any existing object created from the previous module will not be
>> affected, they'll still hold the previous code. "reload" solves some
>> problems, but bring others, especially for the newcomer.
>
> Guido removed it in 3.x because it is badly flawed and he could see any
> way to sensibly fix it.

Well, "moved" rather than "removed":

<dump>
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52)
Type "help", "copyright", "credits" or "license"
>>> import imp
>>> imp.reload
<built-in function reload>
>>>

</dump>

TJG

[toc] | [prev] | [next] | [standalone]


#2271

FromRitesh Nadhani <riteshn@gmail.com>
Date2011-03-30 21:22 -0700
Message-ID<mailman.26.1301545343.2990.python-list@python.org>
In reply to#2203
On Tue, Mar 29, 2011 at 6:14 PM, monkeys paw <monkey@joemoney.net> wrote:
> How do i delete a module namespace once it has been imported?
>
> I use
>
> import banner
>
> Then i make a modification to banner.py. When i import it again,
> the new changes are not reflected. Is there a global variable i can
> modify?
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Have a look at:

http://washort.twistedmatrix.com/2011/01/introducing-exocet.html

-- 
Ritesh
http://www.beamto.us

[toc] | [prev] | [next] | [standalone]


#2273

Fromrusi <rustompmody@gmail.com>
Date2011-03-30 22:11 -0700
Message-ID<1755054f-efe9-408b-88cb-69e5dd6c08c8@k3g2000prl.googlegroups.com>
In reply to#2203
On Mar 30, 6:14 am, monkeys paw <mon...@joemoney.net> wrote:
> How do i delete a module namespace once it has been imported?
>
> I use
>
> import banner
>
> Then i make a modification to banner.py. When i import it again,
> the new changes are not reflected. Is there a global variable i can
> modify?

It seems you are asking about modules and namespaces whereas you
actually want to ask about how to optimize your development
environment -- Yes?
IOW a programmer normally starts with a vague idea, moves to/through
increasing details with the implementation moving from incomplete to
buggy to finished.

Modules (and shrink wrapping in general) is good in the later stages
and a nuisance in the earlier.
As an alternative if you use emacs and python-mode then you can hack
away with C-c C-c (py-execute-buffer) without the problem you
describe.  Then module-arize when done.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web