0

Today, When i do some testing in "python manage.py shell", the global var can't referenced, see:


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**

Every global var i try to reference are face the same error. But put the code in views.py, it works.... How could i fix the django shell?

4

1 回答 1

1

I tried the equivalent of your code in python 2.6 and django 1.3 and it works fine. Not suggesting you downrev, but at least this might be a clue. Also, if you are just using s, then you don't need the global declaration inside your function. I think you only need that if you are modifying 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]: 0
于 2013-06-01T02:52:53.367 回答