Solved

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

Posted on 2004-08-31
3
225 Views
Last Modified: 2010-04-16
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
Comment
Question by:s1m0ne
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
rjkimble earned 100 total points
ID: 11949081
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
 
LVL 1

Author Comment

by:s1m0ne
ID: 11999350
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
 
LVL 9

Expert Comment

by:rjkimble
ID: 11999453
>>> reload(mymodule)
>>> from mymodule import *

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python tuples 2 133
Need help in achieving fail-over / HA in Ubuntu Server. 6 48
Merge XML files using a unique key? 8 116
What is Python programming? 3 130
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question