我的项目有以下布局:
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代码应该记录Cartesian
和xyz_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_functions
和chemcoord.Cartesian
的部分是文档化的,但是我得到了一个关于文档化部分的ImportError:
chemcoord.xyz_functions
和chemcoord.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
我在斯芬克斯的网页上创建了一个问题,他们给了我一个有效的解决方案。我仍然不确定,如果这是一个干净的解决办法,虽然。你知道吗
诀窍是在
__init__.py
文件中使用以下行来伪造模块系统:解释是straightforward:
模块
X
中的import Y
使Y
成为X
模块的属性。它并不意味着将Y
转换为X.Y
模块。 另一方面,import X.Y
尝试加载X.Y
模块,而不是X
模块的Y
属性。 因此,它无法导入。你知道吗包
chemcoord.xyz_functions
和chemcoord.pew
只是对已经记录的包chemcoord.cartesian_coordinates.xyz_functions
的引用(或别名)。你知道吗这就是为什么它不起作用。你知道吗
相关问题 更多 >
编程相关推荐