How to clean python interpreter's environment?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rafal Kleger-Rudomin

    How to clean python interpreter's environment?

    Hello,

    I'm looking for a command to reset interpreter's environment i.e. unload
    all modules, delete variables etc.

    Regards,
    Rafal
  • Peter Hansen

    #2
    Re: How to clean python interpreter's environment?

    Rafal Kleger-Rudomin wrote:[color=blue]
    > I'm looking for a command to reset interpreter's environment i.e. unload
    > all modules, delete variables etc.[/color]

    If you're in the interactive interpreter, you should use
    the following command: ^Z (on Windows), or ^D (Linux).

    (Translation: there's no existing command that does what
    you want. Maybe describing your reason for wanting this will
    let people suggest alternative solutions.)

    -Peter

    Comment

    • Reinhold Birkenfeld

      #3
      Re: How to clean python interpreter's environment?

      Peter Hansen wrote:[color=blue]
      > Rafal Kleger-Rudomin wrote:[color=green]
      >> I'm looking for a command to reset interpreter's environment i.e. unload
      >> all modules, delete variables etc.[/color]
      >
      > If you're in the interactive interpreter, you should use
      > the following command: ^Z (on Windows), or ^D (Linux).
      >
      > (Translation: there's no existing command that does what
      > you want. Maybe describing your reason for wanting this will
      > let people suggest alternative solutions.)[/color]

      What about something like this:

      def clear(keep=("__ builtins__", "clear")):
      keeps = {}
      for name, value in globals().iteri tems():
      if name in keep: keeps[name] = value
      globals().clear ()
      for name, value in keeps.iteritems ():
      globals()[name] = value

      Reinhold

      --
      Wenn eine Linuxdistributi on so wenig brauchbare Software wie Windows
      mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
      "kompletten Betriebssystems " ist, nennt man bei Linux eine Rescuedisk.
      -- David Kastrup in de.comp.os.unix .linux.misc

      Comment

      • Peter Hansen

        #4
        Re: How to clean python interpreter's environment?

        Reinhold Birkenfeld wrote:
        [color=blue][color=green]
        >>Rafal Kleger-Rudomin wrote:
        >>[color=darkred]
        >>>I'm looking for a command to reset interpreter's environment i.e. unload
        >>>all modules, delete variables etc.[/color][/color]
        >
        > What about something like this:
        >
        > def clear(keep=("__ builtins__", "clear")):
        > keeps = {}
        > for name, value in globals().iteri tems():
        > if name in keep: keeps[name] = value
        > globals().clear ()
        > for name, value in keeps.iteritems ():
        > globals()[name] = value[/color]

        Well, that only removes all the references from the
        globals of the current module. Is that all that's
        wanted?

        Note that threads that are already running will not be
        removed, nothing in sys.modules will be removed, and
        there are doubtless a few other things in the interpreter
        that aren't quite so easy to get at.

        -Peter

        Comment

        • Reinhold Birkenfeld

          #5
          Re: How to clean python interpreter's environment?

          Peter Hansen wrote:[color=blue]
          > Reinhold Birkenfeld wrote:
          >[color=green][color=darkred]
          >>>Rafal Kleger-Rudomin wrote:
          >>>
          >>>>I'm looking for a command to reset interpreter's environment i.e. unload
          >>>>all modules, delete variables etc.[/color]
          >>
          >> What about something like this:
          >>
          >> def clear(keep=("__ builtins__", "clear")):
          >> keeps = {}
          >> for name, value in globals().iteri tems():
          >> if name in keep: keeps[name] = value
          >> globals().clear ()
          >> for name, value in keeps.iteritems ():
          >> globals()[name] = value[/color]
          >
          > Well, that only removes all the references from the
          > globals of the current module. Is that all that's
          > wanted?
          >
          > Note that threads that are already running will not be
          > removed, nothing in sys.modules will be removed, and
          > there are doubtless a few other things in the interpreter
          > that aren't quite so easy to get at.[/color]

          Right. So I agree with you on ^D.

          Reinhold

          --
          Wenn eine Linuxdistributi on so wenig brauchbare Software wie Windows
          mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
          "kompletten Betriebssystems " ist, nennt man bei Linux eine Rescuedisk.
          -- David Kastrup in de.comp.os.unix .linux.misc

          Comment

          • Rafal Kleger-Rudomin

            #6
            Re: How to clean python interpreter's environment?

            Reinhold Birkenfeld wrote:[color=blue]
            > Peter Hansen wrote:
            >[color=green]
            >>Reinhold Birkenfeld wrote:
            >>
            >>[color=darkred]
            >>>>Rafal Kleger-Rudomin wrote:
            >>>>
            >>>>
            >>>>>I'm looking for a command to reset interpreter's environment i.e. unload
            >>>>>all modules, delete variables etc.
            >>>
            >>>What about something like this:
            >>>
            >>>def clear(keep=("__ builtins__", "clear")):
            >>> keeps = {}
            >>> for name, value in globals().iteri tems():
            >>> if name in keep: keeps[name] = value
            >>> globals().clear ()
            >>> for name, value in keeps.iteritems ():
            >>> globals()[name] = value[/color]
            >>
            >>Well, that only removes all the references from the
            >>globals of the current module. Is that all that's
            >>wanted?
            >>
            >>Note that threads that are already running will not be
            >>removed, nothing in sys.modules will be removed, and
            >>there are doubtless a few other things in the interpreter
            >>that aren't quite so easy to get at.[/color]
            >
            >
            > Right. So I agree with you on ^D.[/color]

            Well, Control-D just exits python, at least on cygwin. That's not what I
            want.

            I should have explained the background of my question:
            I write some Python app on Windows using PythonWin IDE. It has own
            Python interpreter window, when I run or debug my app, it runs in that
            interpreter environment. But, after the first run, the environment is
            polluted with modules, vars etc.
            What I can do at this moment:
            - Exit IDE and run again every time I run/debug my app.
            - Run my app in external interpreter (but then I cannot debug with IDE)

            Another thing:
            Because of this 'cleaning problem', I write all my classes in the main
            file. I should put them in a module. But then I have to manually reload
            the module each time I edit it. Moreover, reload() also does not do the
            job perfectly. So a 'clean' command would be handy.

            Do you know any developers' guide addressing such work practices,
            practical tricks, project organisation? I have a book 'Python 2.1
            Bible', it is cool but does not go so far.

            Best Regards,
            Rafal

            Comment

            Working...