今天,当我在"python“中进行一些测试时,全局var无法引用,请参见:
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壳?
发布于 2013-06-01 02:52:53
我在python2.6和django 1.3中尝试了与您的代码相当的代码,它运行得很好。不是暗示你放弃,但至少这可能是个线索。另外,如果您只是使用s,那么您不需要函数内部的全局声明。我想你只有在修改s的时候才需要那个。
In [60]: import subprocess as s
In [61]: def test():
global s
f = s.check_call('ls /tmp', shell=True)
return f
....:
In [65]: test()
Out[65]: 0https://stackoverflow.com/questions/16868481
复制相似问题