Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

How to Re-Import a Module after Modifying or Adding a Function Without Exiting the Interpreter

Hello,

When I am testing my functions (from, e.g., mymodule.py) in the Python interpreter, and I edit a function or add a new function, how can I re-import the functions without leaving the interpreter?

If I do

>>> from mymodule import *

again, the interpreter does not recognize the changes. If I exit and then restart the interpreter, it will import the latest changes, but then I lose any working variables as well as my command history.

Is there any way around this?

Thanks in advance,

s1m0ne
0
s1m0ne
Asked:
s1m0ne
  • 2
1 Solution
 
rjkimbleCommented:
AFAIK, you cannot "re-import" a module if you load it the way you describe -- I've even seen it written in documentation that it's not possible. However, you can if you do this:

import mymodule

You can reload it this way:

reload(mymodule)

But that probably won't achieve the effect you're looking for. If you're using IDLE, you can restart the session and then use the session history to re-execute commands you have already run, but that's as close as I have come to achieving what you describe.
0
 
s1m0neAuthor Commented:
rjkimble,

Sorry for the delay. This is the first time I had a chance to test your advice.

Also, I should have specified that I am using the Linux version of Python 2.3.4.

The good news is that your advice works perfectly!

All I have to do after adding or modifying a function is

>>> reload(mymodule)

>>> from mymodule import *

Thanks!

s1m0ne
0
 
rjkimbleCommented:
>>> reload(mymodule)
>>> from mymodule import *

Holy crow! I have never tried that combo before. What a great observation. Thanks!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now