[英]ImportError when using import as
I have the following layout for my project: 我的项目有以下布局:
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
...
...
...
My package has a setup.py
script, was installed via pip install -e
and is available in the PYTHONPATH
. 我的软件包有一个
setup.py
脚本,是通过pip install -e
,可以在PYTHONPATH
。 Nevertheless I also put: sys.path.insert(0, os.path.abspath(u'../../'))
into the Sphinx conf.py
file. 不过我还把:
sys.path.insert(0, os.path.abspath(u'../../'))
放到Sphinx conf.py
文件中。
In the __init__.py
I import: 在
__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
One function in xyz_functions.py
is called eg view
. xyz_functions.py
一个函数被称为例如view
。 And if I do this in my Ipython console all functions are defined: 如果我在Ipython控制台中执行此操作,则定义所有函数:
import chemcoord as cc
cc.cartesian_coordinates.xyz_functions.view
cc.xyz_functions.view
cc.pew.view
The following sphinx code in the cartesian_coordinates.rst
file should document the Cartesian
and the xyz_functions
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
I generate the stub rst
files via sphinx-autogen
and they look like: 我生成存根
rst
通过文件的sphinx-autogen
和他们看起来像:
chemcoord.cartesian_coordinates.xyz_functions.view
==================================================
.. currentmodule:: chemcoord.cartesian_coordinates.xyz_functions
.. autofunction:: view
Now the really strange thing is, that the parts with: chemcoord.cartesian_coordinates.xyz_functions
and chemcoord.Cartesian
are documented, but I get an ImportError for the documentation parts with: chemcoord.xyz_functions
and chemcoord.pew
and they are not documented. 现在真正奇怪的是,记录了
chemcoord.cartesian_coordinates.xyz_functions
和chemcoord.Cartesian
部分,但是我得到了文档部分的ImportError: chemcoord.xyz_functions
和chemcoord.pew
,它们没有记录。 The stub rst
files were created by sphinx-autogen
in all cases. 存根
rst
文件被创建的sphinx-autogen
在所有情况下。 Does anyone have an Idea how to tackle this problem? 有没有人有一个想法如何解决这个问题?
The intended usage for the end user is: 最终用户的预期用途是:
import chemcoord as cc
cc.xyz_functions.view(...)
For this reason I want to document it with xyz_functions
in the namespace of chemcoord
. 为此,我想记录它
xyz_functions
中的命名空间chemcoord
。
Edit 1 (Clarifying because of answer of @LaurentLaport): 编辑1(因为@LaurentLaport的答案而澄清):
Also if I write in the cartesian_coordinates.rst
file the following, it still does not work: 此外,如果我在
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
The packages chemcoord.xyz_functions
and chemcoord.pew
are only references (or aliases) to the package chemcoord.cartesian_coordinates.xyz_functions
which is already documented. chemcoord.xyz_functions
和chemcoord.pew
软件包只是已经记录的chemcoord.cartesian_coordinates.xyz_functions
包的引用(或别名)。
This is why it doesn't work. 这就是为什么它不起作用。
I created an Issue on the sphinx webpage where they gave me a working solution. 我在sphinx网页上创建了一个问题,他们给了我一个有效的解决方案。 I am still not sure, if it is a clean solution though.
我仍然不确定,如果它是一个干净的解决方案。
The trick was to fake the module system with the following lines in the __init__.py
file: 诀窍是在
__init__.py
文件中使用以下行伪造模块系统:
import sys
sys.modules['chemcoord.xyz_functions'] = xyz_functions
The explanation is straightforward : 解释很简单 :
import Y
in module X
makes Y
an attribute of X
module. 模块
X
import Y
使Y
成为X
模块的属性。 It does not mean converting Y
to XY
module. 这并不意味着将
Y
转换为XY
模块。 On the other hand, import XY
tries to load XY
module, not Y
attribute of X
module. 另一方面,
import XY
尝试加载XY
模块,而不是X
模块的Y
属性。 As a result, it failed to import. 结果,它无法导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.