How to force rereading of a changed module in python

By: Python Documentation Team Emailed: 1587 times Printed: 2037 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

For reasons of efficiency as well as consistency, Python only reads the module file on the first time a module is imported. If it didn’t, in a program consisting of many modules where each one imports the same basic module, the basic module would be parsed and re-parsed many times. To force rereading of a changed module, do this:

import imp
import modname
imp.reload(modname)

Warning: this technique is not 100% fool-proof. In particular, modules containing statements like

from modname import some_objects
will continue to work with the old version of the imported objects. If the module contains class definitions, existing class instances will not be updated to use the new class definition. This can result in the following paradoxical behaviour:

>>> import imp
>>> import cls
>>> c = cls.C()                # Create an instance of C
>>> imp.reload(cls)
<module 'cls' from 'cls.py'>
>>> isinstance(c, cls.C)       # isinstance is false?!?
False

The nature of the problem is made clear if you print out the “identity” of the class objects:

>>> hex(id(c.__class__))
'0x7352a0'
>>> hex(id(cls.C))
'0x4198d0'


Python Home | All Python Tutorials | Latest Python Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Python Documentation Team
with in Python
goto in Python
Garbage Collection in Python
switch case statement in Python
Overloading in python
Static in python
Delegation in python
Schwartzian Transform in python
Multidimensional list (array) in python
Array in python
Remove duplicates from a list in python
Iterate over a sequence in reverse order in python
Convert between tuples and lists in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert number to string in python

More Tutorials in Python
What is the need for Python language?
How to compile python script and create .pyc file?
How to find the current module name in python
How to force rereading of a changed module in python
UnboundLocalError in python
call by reference in python
Callable objects in python
Ternary operator in python
Hexadecimal and Octal integers in python
Convert string to number in python
Convert number to string in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert between tuples and lists in python
Iterate over a sequence in reverse order in python
Remove duplicates from a list in python

More Latest News
Most Viewed Articles (in Python )
What is the need for Python language?
Installing gedit for python programming in Windows
Overloading in python
How to compile python script and create .pyc file?
Iterate over a sequence in reverse order in python
Installing gedit for python programming in Linux
Unknown command: 'migrate'
Delegation in python
goto in Python
Comments And Pound Characters in Python
How to force rereading of a changed module in python
Ternary operator in python
Convert string to number in python
Convert number to string in python
Convert between tuples and lists in python
Most Emailed Articles (in Python)
Formatted printing in Python
Delegation in python
Your First Program in Python
Variables And Printing in Python
Convert string to number in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Installing gedit for python programming in Linux
Variables And Names in Python
Strings And Text in Python
Using Unicode Literals in Python
How to force rereading of a changed module in python
Iterate over a sequence in reverse order in python
Remove duplicates from a list in python
Static in python
Comments And Pound Characters in Python