代码之家  ›  专栏  ›  技术社区  ›  Nick X Tsui

iPython笔记本(Python 3):导入错误:没有命名的模块

  •  1
  • Nick X Tsui  · 技术社区  · 8 年前

    首先,我真的是一个python白痴,这是我的第一次python测试。

    我正在运行一个 test_predictors.ipynb 文件使用 Jupyter . 在执行 按块归档,如下所示:

    enter image description here

    这个 decisioni_tree.py test_预测值。ipynb . calculate_information_gain, decision_tree_train, decision_tree_predict 决策树。py公司 . 下图显示了文件布局:

    enter image description here

    我搜索了很多帖子,试着把所有的帖子都放进去。将文件复制到子文件夹中,或在前面添加前导点 decisioni_tree ,或将完整路径添加到 决策树 ,但这些都不起作用。

    PEP

    我使用的是Windows 10,我认为安装了Python 3.4/3.5。

    2 回复  |  直到 8 年前
        1
  •  1
  •   lucians    8 年前

    试试这个:

    sys.path.insert(0, 'directory_of_the_pyfile_you_want_to_import')
    
    import FOLDER.file
    

    哪里 FOLDER file is the .py file 您要导入。

    __init__.py 解决方案无效。。

        2
  •  1
  •   desertnaut SKZI    8 年前

    我建议您通过在笔记本中运行来查看python路径:

    import sys
    print sys.path
    

    如果您的主目录或笔记本目录 'C:\\Users\\yourusername\\.ipython' 尝试将主目录添加到路径:

    sys.path.append('C:\\Users\\yourusername\\')
    

    sys.path.insert(1, 'C:\\Users\\yourusername\\')