I know how to reload a regular Python module within a regular Python interpreter session. This question documents how to do that pretty well:
How do I unload (reload) a Python module?
For some reason, I am having trouble doing that within Django's "manage.py shell" interpreter session. To recreate my issue, start the basic Django tutorial found here:
Writing your first Django app, part 1
After creating the "polls" application and "Poll" class, start up the interpreter via "manage.py shell" and import the "polls" app into it.
import polls.models as pm
Create a new "Poll" object:
p = pm.Poll()
All is well and good so far. Now go back to your source and add any arbitrary method or attribute. For example, I've added:
def x(self):
return 2+2
Now go back to the interpreter and "reload" the module:
reload(pm)
Now try to use your new method or attribute:
p1 = pm.Poll()
p1.x()
You'll get this message:
'Poll' object has no attribute 'x'
What gives? I've also tried rerunning the import command, importing the module using different syntax, deleting all references to any "Poll" objects or to the "Poll" class. I've also tried this with both the IPython interpreter and with the plain Python (v2.6) interpreter. Nothing seems to work.
Using the same techniques with an arbitrary Python module in a regular interpreter session works perfectly. I just can't seem to get it to work in Django's "shell" session.
By the way, if it makes any difference, I'm doing this on a Ubuntu 9.04 machine.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…