Найти - Пользователи
Полная версия: NameError: global name is not defined
Начало » Python для новичков » NameError: global name is not defined
1
Nikulinas
Есть 2 файла:

1. test.py:

import os, os.path
def test_func():
    os.path.exists('s')
test_func()

2. test_main.py

def read_file(path):
  with (open(path)) as f:
    return  ''.join(f.readlines())
exec(read_file('test.py'))
def test():
  exec(read_file('test.py'))
test()

Если запустить ‘python test_main.py’, получаем ошибку:

Traceback (most recent call last):
File “test_main.py”, line 9, in <module>
test()
File “test_main.py”, line 7, in test
exec(read_file('test.py'))
File “<string>”, line 7, in <module>
File “<string>”, line 5, in test_func
NameError: global name ‘os’ is not defined

Но если раскоментарить #exec(read_file('test.py')) в test_main.py, скрипт успешно выполнится.
Не могу понять где ошибка.
argz
Проблема в том, что в файле test_main.py нет строчки import os, os.path.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB