简体   繁体   English

从AB导入X使用__import__?

[英]from A.B import X using __import__?

Let's say A is a package directory, B is a module within the directory, and X is a function or variable written in B. How can I import X using the __import__() syntax? 假设A是包目录,B是目录中的模块,X是用B编写的函数或变量。如何使用__import__()语法导入X? Using scipy as an example: 以scipy为例:

What I want: 我想要的是:

from scipy.constants.constants import yotta

What doesn't work: 什么行不通:

>>> __import__("yotta", fromlist="scipy.constants.constants")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named yotta

>>> __import__("yotta", fromlist=["scipy.constants.constants"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named yotta

>>> __import__("yotta", fromlist=["scipy","constants","constants"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named yotta

>>> __import__("scipy.constants.constants.yotta", fromlist=["scipy.constants.constats"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named yotta

Any suggestions would be much appreciated. 任何建议将不胜感激。

The python import statement performs two tasks: loading the module and makeing it available in the namespace. python import语句执行两个任务:加载模块并使其在命名空间中可用。

import foo.bar.baz 

will provide the name foo in the namespace, not baz , so __import__ will give you foo 将在命名空间中提供名称foo ,而不是baz ,所以__import__会给你foo

foo = __import__('foo.bar.baz')

On the other hand 另一方面

from foo.bar.baz import a, b

does not make a module available, but what the import statement needs to perform the assignmaents is baz. 没有使模块可用,但import语句执行assignmaents需要的是baz。 this corresponds to 这对应于

_tmp_baz = __import__('foo.bar.baz', fromlist=['a', 'b'])
a = _tmp_baz.a
b = _tmp_baz.b

without making the temporary visible, of course. 当然,没有使临时可见。

the __import__ function does not enforce the presence of a and b , so when you want baz you can just give anything in the fromlist argument to put __import__ in the "from input" mode. __import__功能并不强制存在ab ,所以当你想baz你可以给在fromlist里争论什么把__import__在“从输入”模式。

So the solution is the following. 所以解决方案如下。 Assuming 'yotta' is given as a string variable, I have used getattr for attribute access. 假设'yotta'作为字符串变量给出,我使用getattr进行属性访问。

yotta = getattr(__import__('scipy.constants.constants', 
                           fromlist=['yotta']), 
                'yotta')
__import__("scipy.constants.constants", fromlist=["yotta"])

The argument fromlist is equivalent to the right hand side of from LHS import RHS . fromlist的参数等同from LHS import RHS右侧


From the docs: 来自文档:

__import__(name[, globals[, locals[, fromlist[, level]]]])

[...] [...]

The fromlist gives the names of objects or submodules that should be imported from the module given by name . fromlist给出了应该从name给出的模块导入的对象或子模块name

[...] [...]

On the other hand, the statement from spam.ham import eggs, sausage as saus results in 另一方面, from spam.ham import eggs, sausage as saus的声明from spam.ham import eggs, sausage as saus结果

 _temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], -1) eggs = _temp.eggs saus = _temp.sausage 

(Emphasis mine.) (强调我的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM