Forgetting an import

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HMS Surprise

    #1

    Forgetting an import

    I imported a set of functions from a file I wrote to interpreter
    shell:

    from myFile import *

    Now if I change functions in this file how can I make python forget it
    so I can force a fresh import?

    thanx,

    jh

  • Larry Bates

    #2
    Re: Forgetting an import

    HMS Surprise wrote:
    I imported a set of functions from a file I wrote to interpreter
    shell:
    >
    from myFile import *
    >
    Now if I change functions in this file how can I make python forget it
    so I can force a fresh import?
    >
    thanx,
    >
    jh
    >
    While there may be ways of doing this, I have found that you are much
    better off opening another window and run your script from the bare
    OS. It will help you catch problems that you may miss in the
    interpreter shell. It also solves this problem completely.

    -Larry

    Comment

    • =?UTF-8?B?UmVuw6kgRmxlc2NoZW5iZXJn?=

      #3
      Re: Forgetting an import

      HMS Surprise schrieb:
      I imported a set of functions from a file I wrote to interpreter
      shell:
      >
      from myFile import *
      >
      Now if I change functions in this file how can I make python forget it
      so I can force a fresh import?
      I think you are looking for reload(). But don't forget to check its
      documentation, there may be some caveats.
      <http://docs.python.org/lib/built-in-funcs.html>

      --
      René

      Comment

      • kyosohma@gmail.com

        #4
        Re: Forgetting an import

        On Jun 12, 11:36 am, HMS Surprise <j...@datavoice int.comwrote:
        I imported a set of functions from a file I wrote to interpreter
        shell:
        >
        from myFile import *
        >
        Now if I change functions in this file how can I make python forget it
        so I can force a fresh import?
        >
        thanx,
        >
        jh
        If you did an import FunctionName, then you could use the "reload"
        built-in. Otherwise, I think you'll just have to restart the shell.

        <code>
        import FunctionName
        # do something
        # reload FunctionName after editing it
        reload(Function Name)
        </code>

        Mike

        Comment

        • Miki

          #5
          Re: Forgetting an import

          Hello,
          I imported a set of functions from a file I wrote to interpreter
          shell:
          >
          from myFile import *
          >
          Now if I change functions in this file how can I make python forget it
          so I can force a fresh import?
          "import *" is evil for many reasons, this is one of them :)
          >>import my_module
          >>my_module.add (1, 1)
          4
          [Hack]
          >>reload(my_mod ule)
          <module 'my_module' from '/tmp/my_module.py'>
          >>my_module.add (1, 1)
          2

          HTH,
          --
          Miki <miki.tebeka@gm ail.com>
          If it won't be simple, it simply won't be. [Hire me, source code]




          Comment

          • HMS Surprise

            #6
            Re: Forgetting an import

            Thanks for the reloads folks. This is a big help as I am running a
            jython based tool (maxq) and re-starting takes awhile.

            Can't seem to make reload work using the form "from dvTime
            import ...". So I am prefacing my calls with the module name now.


            thanx,
            jh

            Comment

            • Gabriel Genellina

              #7
              Re: Forgetting an import

              En Tue, 12 Jun 2007 15:03:18 -0300, HMS Surprise <john@datavoice int.com>
              escribió:
              Thanks for the reloads folks. This is a big help as I am running a
              jython based tool (maxq) and re-starting takes awhile.
              >
              Can't seem to make reload work using the form "from dvTime
              import ...". So I am prefacing my calls with the module name now.
              This is one of the problems with reload. In that case you would need to
              reload(dvTime) and then re-import all the functions. Using module.function
              avoids that particular problem, as you have noticed.
              With classes it gets worse: any previously created instances will still be
              instances of the *old* class. I think that there are some recipes in the
              Python Cookbook about this problem.

              --
              Gabriel Genellina

              Comment

              Working...