【问题标题】:ImportError in Jupyter NotebookJupyter Notebook 中的 ImportError
【发布时间】:2018-05-12 01:42:08
【问题描述】:

我正在尝试在 Jupyter 中导入一个模块,但它不工作:

import alyn

ImportError                               Traceback (most recent call last)
<ipython-input-8-8e9535ea4303> in <module>()
----> 1 import alyn

~\Anaconda3\envs\tracx\lib\site-packages\alyn\__init__.py in <module>()
      1 """ Import required modules"""
----> 2 from deskew import *
      3 from skew_detect import *

ImportError: No module named 'deskew'

我不太明白为什么,因为有问题的包有一个正确的 init.py 文件:

其内容是:

""" Import required modules"""
from deskew import *
from skew_detect import * 

我错过了什么?

附: 这一切都发生在 Windows 10 上。

【问题讨论】:

    标签: python import dependencies jupyter-notebook packages


    【解决方案1】:

    嗯,我想通了!

    原来我试图导入的包是用 Python 2 编写的,它的 init 文件使用了相对导入机制。但是,我在 Python 3 中工作,并且不再支持相对导入。通过添加 .init 文件可以使 init 文件在 Python 3 中工作。在这两行中,像这样:

    """ Import required modules"""
    from .deskew import *
    from .skew_detect import * 
    

    认为这应该与 Python 2 向后兼容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      相关资源
      最近更新 更多