【发布时间】:2013-06-01 02:23:28
【问题描述】:
今天,当我在“python manage.py shell”中进行一些测试时,无法引用全局变量,请参阅:
In [9]: import subprocess as s
In [10]: def test():
global s
f = s.check_output('ls /tmp', shell=True)
return f
....:
In [11]: test()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 test()
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in test()
1 def test():
2 global s
----> 3 f = s.check_output('ls /tmp', shell=True)
4 return f
**NameError: global name 's' is not defined**
我尝试引用的每个全局变量都面临相同的错误。 但是将代码放在views.py中,它可以工作...... 我该如何修复 django shell?
【问题讨论】:
标签: django shell global-variables