5

I'm running Django 1.5.1, Python 2.7.2, and IPython 0.13.2. If I do "python ./manage.py shell" from within my Django project directory, I get the following error:

from django import forms
class CommentForm(forms.Form):
    name = forms.CharField()

NameError: name 'forms' is not defined.

I know forms is defined as I can see it when it do "dir(forms)". I've noticed that this error only occurs when I'm running iPython within the REPL. If I start the REPL and only use the plain, old Python interpreter, the error doesn't occur.

Has anyone else experienced this problem? If so, do you know why it's occurring and whether or not there's a work-around?

I should add that I've had problems in the past creating classes in the REPL until I created a meta class within the class and defined an "app_label" variable. That didn't make a difference in this situation.

Thanks.

minrk
  • 37,545
  • 9
  • 92
  • 87
Jim
  • 13,430
  • 26
  • 104
  • 155
  • Do you have a file named `forms.py` by any chance in your project folder? – eandersson Apr 07 '13 at 20:55
  • Django has a habit of swallowing legitimate exceptions and re-raising generic ones. I would have a look at code and even modify it to see what the actual exception being raised might be. – aychedee Apr 07 '13 at 20:58
  • eandersson, No, I didn't have a forms.py file in the project but adding one didn't fix the problem. Thanks for the suggestion. – Jim Apr 07 '13 at 21:33

1 Answers1

8

django 1.5 doesn't start IPython properly. This is fixed in master, but the fix was not backported to 1.5.1. If you manually apply that patch to core/management/commands/shell.py, IPython should work as expected.

minrk
  • 37,545
  • 9
  • 92
  • 87
  • 1
    I monkey-patched shell.py and that fixed the problem. Thank you very much. – Jim Apr 07 '13 at 22:41
  • Big thank you for this! One note for other monkey-patchers: In my version of iPython at least, I got a deprecation warning on the 'frontend' package. You can make that warning go away by simply removing the 'frontend' layer: from IPython.terminal.ipapp import TerminalIPythonApp instead of from IPython.frontend.terminal.ipapp import TerminalIPythonApp – AC Capehart Feb 04 '14 at 20:14