[英]How to use from x import y using importlib in Python
I wanted to install and import a package from script.我想从脚本安装和导入一个包。 Then I wanted to add
from x import y
and from x import *
in that script.然后我想在该脚本中添加
from x import y
和from x import *
。 (I was making some functions actually) . (我实际上是在做一些功能) 。
I successfully installed and imported using subprocess
and importlib
.我成功地安装和使用进口的
subprocess
和importlib
。 But I'm at a loss in from x import y
and from x import *
.但是我在
from x import y
和from x import *
不知所措。 Tried these codes and some others.尝试了这些代码和其他一些代码。
globals()[package] = importlib.import_module('package','class')
globals()[package] = importlib.import_module('class','package')
globals()[package] = importlib.import_module('package','*')
globals()[package] = importlib.import_module('*','package')
importlib.import_module('gtts','gTTS')
importlib.import_module('gtts','*')
But they didn't work.但他们没有工作。 Shows:
节目:
NameError: name 'gTTS' is not defined
ModuleNotFoundError: No module named 'gTTS'
etc.等等。
I don't think you can load methods directly, since this method just loads modules.我不认为你可以直接加载方法,因为这个方法只是加载模块。 A module is loaded with
import module
and i defined as "A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. " (check documentation ).一个模块加载了
import module
,我定义为“模块是一个包含 Python 定义和语句的文件。文件名是后缀 .py 的模块名称。”(检查文档)。 from x import y
does also do the import module
but sets another namespace (see discussion here ). from x import y
也执行import module
但设置了另一个命名空间(请参阅此处的讨论)。 What you can do is load your module and then set your namespace manually afterwards, this is what the from x import y syntax
does.你可以做的是加载你的模块,然后手动设置你的命名空间,这就是
from x import y syntax
所做的。 You can load a module with the path (in this example i want to load read_csv
from pandas):您可以使用路径加载模块(在本例中,我想从
read_csv
加载read_csv
):
importlib.import_module(".io.parsers", "pandas")
(check the path with print(inspect.getmodule(read_csv).__name__)
) (用
print(inspect.getmodule(read_csv).__name__)
检查路径)
Also a solution like jottbe mentioned in the commentary would be possible评论中提到的jottbe 等解决方案也是可能的
The exact syntax to correspond to mischva11's answer, is as follows:与 mischva11 的答案相对应的确切语法如下:
tempmod = importlib.import_module("x_mymodule")
y = x_mymodule.y
del tempmod # optionally delete the reference to the parent module
Imports only the attribute y
from the module x_mymod
, similar to from x_mymodule import y
.仅导入属性
y
从模块x_mymod
,类似于from x_mymodule import y
。 You can get the ... as z
functionality by assigning it to any name you want in the 2nd line of code.您可以通过在第二行代码中将其分配给您想要的任何名称来获得
... as z
功能。
I found it useful to delete the parent module from the namespace, when using this in a sub-module, to prevent cluttering up the namespace.我发现从命名空间中删除父模块很有用,当在子模块中使用它时,可以防止命名空间混乱。
I think to get the import *
function you'll have to iterate through the module's attributes and assign them to a named attribute.我认为要获得
import *
函数,您必须遍历模块的属性并将它们分配给命名属性。 Here are two hints that help you get there:这里有两个提示可以帮助您实现目标:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.