UnboundLocalError on nested module reimport

后端 未结 2 514
刺人心
刺人心 2021-01-18 10:10

I get an UnboundLocalError when I reimport an already imported module in python 2.7. A minimal example is

#!/usr/bin/python

import sys

def foo():
    print         


        
2条回答
  •  走了就别回头了
    2021-01-18 11:02

    What's hard to understand about this situation is that when you import something inside a scope, there is an implicit assignment. (Actually a re-assignment in this case).

    The fact that import sys exists within foo means that, within foo, sys doesn't refer to the global sys variable, it refers to a separate local variable also called sys.

提交回复
热议问题