首页 > 解决方案 > .py 中的相对导入正在杀死我

问题描述

我正在尝试使用以下文件夹结构编写单元测试(是的,我知道这太过分了,我只是想学习):

sentence_checker
├───__init__.py
|
├───project_files
|   ├───__init__.py 
|   └───sentence_format_checker.py
|
└───tests
    ├───__init__.py
    └───test_sentence_format_checker.py

在我的测试文件中,我有以下几行:

import unittest
from ..project_files import test_sentence_format_checker    # also the name of the function I'm testing

当我运行时:

python -m unittest test_sentence_format_checker.py

我明白了ImportError: attempted relative import with no known parent directory

我在这里和其他地方尝试了一些不同的变体和大量的旧搜索,但我无法对其进行排序。任何人都可以帮忙吗?

标签: python

解决方案


推荐阅读