我在使用“import module”而不是“from module import ...”语法时遇到了问题。这清楚地表明我对加载模块的理解是不够的。据我在其他地方发现,这种差异主要是样式问题,但这并不能解释以下情况。
我安装了 ase 使用
sudo apt install python3-ase
我尝试了以下方法:
import ase
ase.io.read
哪个输出
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'ase' has no attribute 'io'
然而,当尝试
from ase.io import read
read
或者——也可能——
from ase.io import read
import ase
ase.io.read
我得到输出
<function read at 0x7f33dc721730>
后者是想要的结果,因为我想使用 ase.io.read 函数来读取 .cif 文件。
有关问题根源的更多信息显示在以下 python 会话中:
import sys
import ase
sys.modules['ase']
来自“/home/vanbeverj/Programs/anaconda3/envs/abienv/lib/python3.6/site-packages/ase/init .py”的模块“ ase ”
dir(ase)
['Atom', 'Atoms', 'LooseVersion', ' all ', ' builtins ' , ' cached ', ' doc ', ' file ', ' loader ', ' name ', ' package ', ' path ', '规格','版本','ase','atom','atoms','calculators','cell','constraints','data','dft','formula','geometry','np' , 'parallel', 'symbols', 'sys', 'transport', 'units', 'utils']
from ase.io import read
dir(ase)
['Atom', 'Atoms', 'LooseVersion', ' all ', ' builtins ' , ' cached ', ' doc ', ' file ', ' loader ', ' name ', ' package ', ' path ', '规格','版本','ase','atom','atoms','calculators','cell','constraints','data','dft','formula','geometry','io' , 'np', 'parallel', 'symbols', 'sys', 'transport', 'units', 'utils']
'dir(ase)' 命令有明显不同的输出。例如 io 子模块会发生什么?有人可以解释一下引擎盖下发生了什么吗?