Найти - Пользователи
Полная версия: Динамическая загрузка кода Python2.6 и Python 3(не работает)
Начало » Python для новичков » Динамическая загрузка кода Python2.6 и Python 3(не работает)
1
YuraL
Помогите разобраться.
Код
import sys, imp
source = """
def bar():
print('bar method of')"""
code = compile(source, 'foo', 'exec')
module = imp.new_module('foo')
exec (code) in module.__dict__
module.bar()
sys.modules['foo'] = module
import foo
foo.bar()
в 2.6 работает а в Питон 3

выдает
File "C:\Python31\im.py", line 8, in <module>
module.bar()
AttributeError: 'module' object has no attribute 'bar'
Посмотрел importlib , но как там загрузить код непонятно

Спасибо, Юрий
YuraL
Разобрался. С помощью утилиты 2to3. Неправильный вызов exec
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