2017-12-01 15 views
1

이 작동파이썬의 간부 실행하지

def run(): 
    import os 
    import sys 
    with open('tests.py') as fptr: 
     script_content = fptr.read() 
    exec(script_content) 

run() 

결과 :

Traceback (most recent call last): 
    File "tmp.py", line 8, in <module> 
    run() 
    File "tmp.py", line 6, in run 
    exec(script_content) 
    File "<string>", line 15, in <module> 
    File "<string>", line 16, in PlaceSpitter 
NameError: name 'Place' is not defined 

사람에게 내 왜, 어떻게 수를 그것을 해결하기 위해?

답변

1

내가 다시 읽은 - carrefully - python's docs 특히이 하나

은 모듈 수준에서 그 기억, 전역과 지역 주민이 동일한 사전을하다

그리고이 시도 :

def run(): 
    import os 
    import sys 
    with open('tests.py') as fptr: 
     script_content = fptr.read() 
    exec(script_content, globals()) 

run() 

이제 작동합니다!

나는 아직도 왜 그런지 모르지만 적어도 지금은 작동합니다.