Solved

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

Posted on 2004-08-31
3
218 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now