使用 import as 时出现 ImportError

ImportError when using import as

我的项目布局如下:

chemcoord/
    __init__.py
    cartesian_coordinates/
        xyz_functions.py
        cartesian_class_main.py
        ...
    ...
docs/
    sources/
        conf.py
        cartesian_coordinates.rst
        src_xyz_function1/
            chemcoord.cartesian_coordinates.xyz_functions.view.rst
            ...
        ...
    ...

我的包有一个 setup.py 脚本,通过 pip install -e 安装并在 PYTHONPATH 中可用。不过我也说: sys.path.insert(0, os.path.abspath(u'../../')) 进入 Sphinx conf.py 文件。

__init__.py我导入:

from chemcoord.cartesian_coordinates.cartesian_class_main import Cartesian
from chemcoord.cartesian_coordinates import xyz_functions
# the import of pew is just for testing purposes
from chemcoord.cartesian_coordinates import xyz_functions as pew

xyz_functions.py 中的一个函数被调用,例如view。 如果我在 Ipython 控制台中执行此操作,则所有函数都已定义:

 import chemcoord as cc
 cc.cartesian_coordinates.xyz_functions.view
 cc.xyz_functions.view
 cc.pew.view

cartesian_coordinates.rst 文件中的以下 sphinx 代码应记录 Cartesianxyz_functions

Cartesian coordinates
===================================



.. currentmodule:: chemcoord


The ``Cartesian`` class which is used to represent
a molecule in cartesian coordinates.

.. autosummary::
    :toctree: src_Cartesian

    ~Cartesian



A collection of functions operating on instances of ``Cartesian``.

.. currentmodule:: chemcoord.cartesian_coordinates.xyz_functions

.. autosummary::
    :toctree: src_xyz_functions1

    ~isclose
    ~read
    ~write
    ~view


A collection of functions operating on instances of ``Cartesian``.

.. currentmodule:: chemcoord

.. autosummary::
    :toctree: src_xyz_functions2

    ~xyz_functions.isclose
    ~xyz_functions.read
    ~xyz_functions.write
    ~xyz_functions.view


A collection of functions operating on instances of ``Cartesian``.

.. currentmodule:: chemcoord

.. autosummary::
    :toctree: src_xyz_functions3

    ~pew.isclose
    ~pew.read
    ~pew.write
    ~pew.view

我通过 sphinx-autogen 生成存根 rst 文件,它们看起来像:

chemcoord.cartesian_coordinates.xyz_functions.view
==================================================

.. currentmodule:: chemcoord.cartesian_coordinates.xyz_functions

.. autofunction:: view

现在真正奇怪的是, 包含 chemcoord.cartesian_coordinates.xyz_functionschemcoord.Cartesian 的部分已记录,但我得到文档部分的 ImportError: chemcoord.xyz_functionschemcoord.pew 并且它们没有记录。在所有情况下,存根 rst 文件都是由 sphinx-autogen 创建的。 有没有人知道如何解决这个问题?

最终用户的预期用途是:

 import chemcoord as cc
 cc.xyz_functions.view(...)

出于这个原因,我想在 chemcoord.

的命名空间中用 xyz_functions 记录它

编辑 1(由于@LaurentLaport 的回答而澄清):

此外,如果我在 cartesian_coordinates.rst 文件中写入以下内容,它仍然不起作用:

Cartesian coordinates
===================================



.. currentmodule:: chemcoord


The ``Cartesian`` class which is used to represent
a molecule in cartesian coordinates.

.. autosummary::
    :toctree: src_Cartesian

    ~Cartesian



A collection of functions operating on instances of ``Cartesian``.

.. currentmodule:: chemcoord

.. autosummary::
    :toctree: src_xyz_functions2

    ~xyz_functions.isclose
    ~xyz_functions.read
    ~xyz_functions.write
    ~xyz_functions.view

chemcoord.xyz_functionschemcoord.pew 只是对包 chemcoord.cartesian_coordinates.xyz_functions 的引用(或别名),已经记录在案。

这就是它不起作用的原因。

我在 sphinx 网页上创建了一个问题,他们给了我一个可行的解决方案。我仍然不确定它是否是一个干净的解决方案。

技巧是在 __init__.py 文件中使用以下行伪造模块系统:

import sys
sys.modules['chemcoord.xyz_functions'] = xyz_functions

解释是straightforward:

模块 X 中的

import Y 使 Y 成为 X 模块的属性。这并不意味着将 Y 转换为 X.Y 模块。 另一方面,import X.Y 尝试加载 X.Y 模块,而不是 X 模块的 Y 属性。 结果导入失败