Python 从A.B导入X使用_导入_;?

Python 从A.B导入X使用_导入_;?,python,import,Python,Import,假设A是包目录,B是目录中的模块,X是用B编写的函数或变量。如何使用\uuuu import\uuuu()语法导入X?以scipy为例: 我想要的是: from scipy.constants.constants import yotta 什么不起作用: >>> __import__("yotta", fromlist="scipy.constants.constants") Traceback (most recent call last): File "<std

假设A是包目录,B是目录中的模块,X是用B编写的函数或变量。如何使用
\uuuu import\uuuu()
语法导入X?以scipy为例:

我想要的是:

from scipy.constants.constants import 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__("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
导入(“yotta”,fromlist=“scipy.constants.constants”) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ImportError:没有名为yotta的模块 >>>导入(“yotta”,fromlist=[“scipy.constants.constants”]) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ImportError:没有名为yotta的模块 >>>导入(“yotta”,fromlist=[“scipy”,“constants”,“constants”]) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ImportError:没有名为yotta的模块 >>>导入(“scipy.constants.constants.yotta”,fromlist=[“scipy.constants.constants”]) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ImportError:没有名为yotta的模块 如有任何建议,将不胜感激

__import__("scipy.constants.constants", fromlist=["yotta"])
参数
fromlist
相当于LHS import RHS中
的右侧


\uuuu导入(名称[,全局[,局部[,来自列表[,级别]]])

[……]

fromlist
给出了对象的名称,或者应该从
name
给出的模块导入的子模块的名称

[……]

另一方面,spam.ham进口鸡蛋、香肠作为saus的声明

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

(Emphasis mine.)

python导入语句执行两项任务:加载模块并使其在名称空间中可用

import foo.bar.baz 
将在名称空间中提供名称
foo
,而不是
baz
,因此
\uuuuuuuuuuuuuuuuu
将为您提供
foo

foo = __import__('foo.bar.baz')
另一方面

from foo.bar.baz import a, b
不会使模块可用,但import语句需要执行赋值操作的是baz。这相当于

_tmp_baz = __import__('foo.bar.baz', fromlist=['a', 'b'])
a = _tmp_baz.a
b = _tmp_baz.b
当然,不需要让临时文件可见

\uuuu import\uuuu
函数不强制存在
a
b
,因此当需要
baz
时,您可以在fromlist参数中给出任何内容,以将
\uu import\uuuu
置于“来自输入”模式

因此,解决方案如下。假设‘yotta’作为字符串变量给出,我使用了
getattr
进行属性访问

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

好啊然后我很困惑我的代码在这一点上是如何工作的。我一直在使用与之等价的:dist=\uuuu import\uuu('scipy.stats.distributions',fromlist=['scipy','stats']),因为如果我不使用fromlist,dis将只是'scipy'。那它是双向的吗?不,不是。我不确定你的代码是如何工作的。这仍然不太正确。如果我说yotta=\uuuu import\uuuuu(等等),yotta就是scipy.constants.constants,为了得到值,你需要执行yotta.yottaYes,因为
fromlist
可以有多个值,它是一个列表。因此,您仍然必须将其中的项分配到各自的内容中,如文档中的示例所示。
\uuuu import\uuuu('scipy.stats.distributions')
返回
scipy
模块,因为这是普通import语句处理包的方式:
import scipy.stats.distributions
放置根目录(
scipy
)进入命名空间,而不是“叶”(
distributions
);它只是确保
scipy.stats.distributions
已加载并准备好访问。但是,如果您提供了
fromlist
,那么“叶”模块将从
\uuuuuu导入()
调用返回。
\uu导入()
并没有真正检查以确保这些名称存在,因此
fromlist
中的几乎所有内容都将起作用。这最终完成了我需要它做的事情。我不能说我完全理解,如果fromlist项不存在(即用'yyyyy'替换'yotta',为什么import语句本身不发出尖叫声)。但是,如果“yyyy”不存在,至少您会得到一个AttributeError。谢谢您的帮助!