如何从Python中的代码对象生成模块对象 [英] How to generate a module object from a code object in Python
问题描述
鉴于我有一个模块的代码对象,如何获得相应的模块对象?
Given that I have the code object for a module, how do I get the corresponding module object?
它看起来像 moduleNames = { }; moduleNames
中的exec代码执行的操作与我想要的非常接近。它将模块中声明的全局变量返回到字典中。但是,如果我想要实际的模块对象,该如何获得它?
It looks like moduleNames = {}; exec code in moduleNames
does something very close to what I want. It returns the globals declared in the module into a dictionary. But if I want the actual module object, how do I get it?
编辑:
看起来您可以滚动自己的模块对象。模块类型不方便记录,但是您可以执行以下操作:
It looks like you can roll your own module object. The module type isn't conveniently documented, but you can do something like this:
import sys
module = sys.__class__
del sys
foo = module('foo', 'Doc string')
foo.__file__ = 'foo.pyc'
exec code in foo.__dict__
推荐答案
正如评论已经表明的那样,在当今的Python中,实例化没有内置名称的类型是调用通过类型模块:
As a comment already indicates, in today's Python the preferred way to instantiate types that don't have built-in names is to call the type obtained via the types module from the standard library:
>>> import types
>>> m = types.ModuleType('m', 'The m module')
请注意,这确实不是会自动将新模块插入 sys.modules
:
note that this does not automatically insert the new module in sys.modules
:
>>> import sys
>>> sys.modules['m']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'm'
这是您必须手动执行的任务:
That's a task you must perform by hand:
>>> sys.modules['m'] = m
>>> sys.modules['m']
<module 'm' (built-in)>
这很重要,因为模块的代码对象通常在之后执行模块已添加到 sys.modules
中-例如,此类代码引用 sys.modules [__ name __] $ c $完全正确c>,如果您忘记了此步骤,将失败(
KeyError
)。 此步骤之后,将 m .__ file __
设置为您已经在编辑中得到的值,
This can be important, since a module's code object normally executes after the module's added to sys.modules
-- for example, it's perfectly correct for such code to refer to sys.modules[__name__]
, and that would fail (KeyError
) if you forgot this step. After this step, and setting m.__file__
as you already have in your edit,
>>> code = compile("a=23", "m.py", "exec")
>>> exec code in m.__dict__
>>> m.a
23
(或Python 3等效项,其中 exec
是一个函数,当然,如果您使用的是Python 3,-)是正确的(当然,通常您会通过精巧的方式而不是编译字符串来获取代码对象,但这就是
(or the Python 3 equivalent where exec
is a function, if Python 3 is what you're using, of course;-) is correct (of course, you'll normally have obtained the code object by subtler means than compiling a string, but that's not material to your question;-).
在旧版本的Python中,您应该使用 new
模块来代替的 types
模块在开始时就创建了一个新的模块对象,但是 new
从Python 2.6开始就已弃用,并在Python 3。
In older versions of Python you would have used the new
module instead of the types
module to make a new module object at the start, but new
is deprecated since Python 2.6 and removed in Python 3.
这篇关于如何从Python中的代码对象生成模块对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!