简体   繁体   English

从另一个目录导入文件时的ImportError

[英]ImportError when importing file from another directory

I am playing around with the AIMA python project, but I'm having trouble with importing the logic.py file into main.py . 我正在玩AIMA python项目,但是在将logic.py文件导入main.py遇到了麻烦。 The following is folder structure: 以下是文件夹结构:

project/
    aima/
        __init__.py
        utils.py
        logic.py
    main.py

I added the folder to my python path variable. 我将该文件夹添加到我的python路径变量中。 Every time I do 每次我做

# main.py
import aima.logic as logic

I get this error: 我收到此错误:

File "main.py", line 2, in import aima.logic as logic File "/project/aima/logic.py", line 34, in from utils import ( ImportError: No module named 'utils' 从utils导入,在导入aima.logic中的文件“ main.py”,第2行,在逻辑文件“ /project/aima/logic.py”,在行34中,导入(ImportError:没有名为“ utils”的模块

I thought this was strange since logic.py imports the utils file it should be fine since they are under the same directory. 我认为这很奇怪,因为logic.py导入了utils文件,应该很好,因为它们位于同一目录下。

I tried searching for answers, but most of them mention adding to python module search path and adding __init__.py and do not work for me. 我尝试搜索答案,但是其中大多数提到添加到python模块搜索路径并添加__init__.py ,对我不起作用。

试试这个可能很好

  from project.aima import logic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM