-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Django shell (IPython) gives NameError on dict comprehensions #2532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, I think this might be a mistake in django :
But my knowledge of django is ~ 0 |
@Carreau you use This is not a problem with Django, because the same syntax works in default python shell, you can try that using I understand the point you are making here, and I am not saying that django is perfect. But, we could investigate a little into this. |
Sorry, I was meaning the way django is launching IPython, I have no idea how manage.py does effectively decide to launch an IPython instance. |
here is where IPython is launched from |
I have IPython v0.13. |
Seem to be what was discussed in #62. |
Hi, yes, we've come across this problem before - the trouble is that Django uses |
This bug has been fixed in django (by me). Closing here. |
This does not appear to be fixed and looks like the same issue as in #62 In [1]: from datetime import datetime
In [2]: def test():
...: print datetime.now()
...:
In [3]: test()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/Users/jneumann/svn/trunk/web/python/python-environments/pffenv/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 test()
/Users/jneumann/svn/trunk/web/python/python-environments/pffenv/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in test()
1 def test():
----> 2 print datetime.now()
3
NameError: global name 'datetime' is not defined
In [4]: names = ['carl', 'jim', 'jack', 'john', 'mark']
In [5]: n=1
In [6]: {name:n for name in names}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/Users/jneumann/svn/trunk/web/python/python-environments/pffenv/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 {name:n for name in names}
/Users/jneumann/svn/trunk/web/python/python-environments/pffenv/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <dictcomp>((name,))
----> 1 {name:n for name in names}
NameError: global name 'n' is not defined Tested this in Django 1.5 and 1.5.1 |
I believe the fix is in django 1.6, not 1.5. Fixed versions of django don't use embedded IPython at all (it never should have), which is the relevant fix. |
Got it. Thanks for the information. Well the easiest way to get around this issue is to just grab the following lines (from here https://github.com/django/django/blob/master/django/core/management/commands/shell.py) and replace the current implementation in your pyenv with this: def ipython(self):
try:
from IPython.frontend.terminal.ipapp import TerminalIPythonApp
app = TerminalIPythonApp.instance()
app.initialize(argv=[])
app.start()
except ImportError:
# IPython < 0.11
# Explicitly pass an empty list as arguments, because otherwise
# IPython would use sys.argv from this script.
try:
from IPython.Shell import IPShell
shell = IPShell(argv=[])
shell.mainloop()
except ImportError:
# IPython not found at all, raise ImportError
raise Which would be located in the same location. Seemed to be a backwards compatible 1.5 change from what I've tested thus far. Thanks for the help @minrk |
However, the same works on default python shell
http://stackoverflow.com/questions/13135145/django-problems-when-using-dict-comprehensions-nameerror-global-name-user-is
The text was updated successfully, but these errors were encountered: