简体   繁体   English

使用导入时的ImportError

[英]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代码应记录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

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_functionschemcoord.Cartesian部分,但是我得到了文档部分的ImportError: chemcoord.xyz_functionschemcoord.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_functionschemcoord.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.

相关问题 ImportError:使用pytest时无法导入名称 - ImportError: cannot import name when using pytest 导入错误:使用 Azure 后端时无法导入名称“BlobService” - ImportError: cannot import name 'BlobService' when using Azure Backend ImportError:使用 certbot 时无法导入名称 UnrewindableBodyError - ImportError: cannot import name UnrewindableBodyError when using certbot ImportError:使用Azure SDK时无法导入名称会话 - ImportError: cannot import name Session, when using Azure SDK 使用具有多个子类的baseclass时,ImportError无法导入名称 - ImportError cannot import name when using baseclass with multiple subclasses 使用restkit时出现错误“ ImportError:无法导入名称SimplePool” - Getting error “ImportError: cannot import name SimplePool” when using restkit ImportError:使用VSCode时无法导入名称“ cntk_device” - ImportError: cannot import name 'cntk_device' when using VSCode 使用PyCharm的Profiler时,“ImportError:无法导入名称cbook” - 'ImportError: cannot import name cbook' when using PyCharm's Profiler ImportError:使用PySide时无法导入名称QtWebKit - ImportError: cannot import name QtWebKit when using PySide 尝试使用脚本在 Atom 中导入熊猫时出现导入错误 - ImportError when trying to import pandas in Atom using script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM