[英]Can importlib.import_module(m) do `from m import *`?
The file module_x
has文件
module_x
有
module_x.py模块_x.py
A = 'x'
And the following script will print 'x' from module_x.py
, which is what I need.下面的脚本将从
module_x.py
打印 'x',这正是我所需要的。
A = 'a'
from module_x import *
print(A) # x
However, I need to dynamically import the module so I use importlib for it.但是,我需要动态导入模块,因此我使用了 importlib。 However, the following code prints 'a'.
但是,以下代码打印“a”。
from importlib import import_module
A = 'a'
m = 'module_x' # m will be passed as a parameter
x = import_module(m)
print(A) # a
The value of xA
is 'x', but I want the import_module()
to assign A to 'x'. xA
的值是“x”,但我希望import_module()
将 A 分配给“x”。
this should do it:这应该这样做:
globals().update(import_module("module").__dict__)
In case you want to import something specific如果您想导入特定的东西
var = import_module("module").var
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.