将导入用作时的导入错误

2024-06-26 10:14:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我的项目有以下布局:

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'../../'))到Sphinxconf.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

Tags: ofpyimportsrcviewrstfunctionscc
2条回答

我在斯芬克斯的网页上创建了一个问题,他们给了我一个有效的解决方案。我仍然不确定,如果这是一个干净的解决办法,虽然。你知道吗

诀窍是在__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属性。 因此,它无法导入。你知道吗

chemcoord.xyz_functionschemcoord.pew只是对已经记录的包chemcoord.cartesian_coordinates.xyz_functions的引用(或别名)。你知道吗

这就是为什么它不起作用。你知道吗

相关问题 更多 >