Solved

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

Posted on 2004-08-31
3
223 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
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…
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 …

856 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