【问题标题】:Why can't I access imported functions in Django's shell with ipython?为什么我不能使用 ipython 在 Django 的 shell 中访问导入的函数?
【发布时间】:2013-10-01 00:22:15
【问题描述】:

我已经在我的 virtualenv 中安装了 ipython,所以python manage.py shell 提供了我的 ipython。但是,我无法从内部函数定义访问导入:

$ 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 中不起作用?

【问题讨论】:

    标签: python django shell ipython


    【解决方案1】:

    这是在 django 1.6 中修复的known bug。问题讨论中建议的早期版本也有一些解决方法,看看吧。

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-02
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2018-06-25
      相关资源
      最近更新 更多