[英]Module not found, but only in ipython3
I have written a package named biographs
with the following architecture: 我编写了一个具有以下架构的名为
biographs
的软件包:
biographs (folder)
>biographs (package)
>__init__.py
>bpdb.py
>pmolecule.py
>bgraph.py
>bspace.py
The __init__.py
file is only the following: __init__.py
文件仅是以下内容:
from .pmolecule import Pmolecule
When I'm working in ipython3 and I want to import biographs
(only to use the class Pmolecule
), I get the following error in ipython3 (Ipython 6.0.0, Python 3.6.1): 当我在ipython3中工作并且想要导入
biographs
(仅使用类Pmolecule
)时,我在ipython3(Ipython 6.0.0,Python 3.6.1)中收到以下错误:
In [1]: cd ~/biographs/
/Users/rdora/biographs
In [2]: import biographs
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-1803e6928e0e> in <module>()
----> 1 import biographs
/Users/rdora/biographs/biographs/__init__.py in <module>()
----> 1 from .pmolecule import Pmolecule
/Users/rdora/biographs/biographs/pmolecule.py in <module>()
1 # class to deal with protein structures
2 # python 2
----> 3 import bpdb
4 import bgraph
5 import bspace
ModuleNotFoundError: No module named 'bpdb'
However when I do exactly the same process using IPython 5.3.0 with Python 2.7.13 there is no error message. 但是,当我将IPython 5.3.0与Python 2.7.13进行完全相同的处理时,没有错误消息。
Thank you 谢谢
This is because of how imports work in Python 2 and Python 3. In your module pmolecule.py
you apparently do import bpdb
. 这是因为导入在Python 2和Python 3中是如何工作的。在模块
pmolecule.py
您显然可以import bpdb
。 In Python 2 this will search the local directory for a module called bpdb.py
and import it. 在Python 2中,这将在本地目录中搜索名为
bpdb.py
的模块并将其导入。 However in Python 3 you must be explicit about those relative imports, ie you need to do 但是,在Python 3中,您必须明确说明这些相对导入,即您需要
from . import bpdb
In order to get consistency for Python 2 you can use from __future__ import absolute_imports
which prohibits such non-explicit imports also under Python 2. 为了获得Python 2的一致性,您可以使用
from __future__ import absolute_imports
,它也禁止在Python 2下进行此类非显式的导入。
Note that the same holds for: 注意:
----> 3 import bpdb
4 import bgraph
5 import bspace
Those need to be imported via from . import <module-name>
这些需要通过导入
from . import <module-name>
from . import <module-name>
syntax. from . import <module-name>
语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.