为什么我不能用ipython访问Django shell中的导入函数? [英] Why can't I access imported functions in Django's shell with ipython?
本文介绍了为什么我不能用ipython访问Django shell中的导入函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在我的virtualenv中安装了ipython,所以 python manage.py shell
给出了我的ipython。但是,我无法从函数定义内部访问导入:
I have installed ipython in my virtualenv, so python manage.py shell
gives my ipython. However, I can't access imports from inside function definitions:
$ python manage.py shell
Python 2.7.5 (default, Sep 6 2013, 09:55:21)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from re import search
In [2]: def my_search(pattern, string):
return search(pattern, string)
...:
In [3]: my_search('x', 'y')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/home/wilfred/.envs/drawbridge/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 my_search('x', 'y')
/home/wilfred/.envs/drawbridge/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in my_search(pattern, string)
1 def my_search(pattern, string):
----> 2 return search(pattern, string)
3
NameError: global name 'search' is not defined
如果我直接启动 ipython
,这样可以正常工作。为什么它不能用于Django shell?
This works fine if I start ipython
directly. Why doesn't it work from the Django shell?
推荐答案
这是已知错误已在django 1.6中修复。在问题讨论中建议的早期版本也有一些解决方法,看看。
This is a known bug that was fixed in django 1.6. There are also some workarounds for earlier versions suggested in the issue discussion, take a look.
另见:
- Variables scope in inline django shell, vs python shell
- Getting NameError with Django 1.5 and IPython
这篇关于为什么我不能用ipython访问Django shell中的导入函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文