Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-31
3
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
rjkimble earned 400 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

670 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