Getting NameError with Django 1.5 and IPython

2019-02-14 01:50发布

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.

1条回答
放我归山
2楼-- · 2019-02-14 02:44

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.

查看更多
登录 后发表回答