首页 > 解决方案 > 对不起另一个python导入问题

问题描述

这是我的项目结构:

 |-project
      |-tests
           |-test1.py   
      |-tools
           |-AuxillaryFunctions

我希望能够从 test1.py 中的 AuxillaryFunctions.py 调用函数。我试过了

from tools import AuxillaryFunctions as AF

from .. import tools.AuxillaryFunctions as AF

from ..tools import AuxillaryFunctions as AF

似乎还有其他一切,但我不断收到 ImportErrors。对不起另一个进口问题,我似乎无法理解这一点。

标签: pythonimport

解决方案


Python 只检测来自同一目录或 pythonpath 目录的模块。

解决方案1:

将“..\tools\AuxillaryFunctions”添加到 PYTHONPATH 环境变量

解决方案2:

将模块路径附加到 sys.path 以在运行时检测模块。

path = (os.path.join(os.path.abspath(os.path.dirname(__file__) + "../../"),
                                             /'tools\\AuxillaryFunctions'))
sys.path.append(path)
from AuxillaryFunctions import AF

推荐阅读