python - .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
。
我在这里和其他地方尝试了一些不同的变体和大量的旧搜索,但我无法对其进行排序。任何人都可以帮忙吗?
解决方案
推荐阅读
- r - 拆分向量和求和值
- gradle - 在以下任何来源中都找不到插件 [id: 'org.jetbrains.kotlin.jvm']
- python-3.x - 简单的函数语句没有给出任何输出
- python - 注册系统不会为我创建我需要的 .txt 文件。Tkinter Python
- python - 为什么第二个嵌套 for 循环在迭代期间跳过重复值?
- bash - 子shell中使用的“basename”返回“command not found”
- javascript - 在 Node.js(电子应用程序)中渲染没有框架的 PDF
- swift - 通过 viewDidAppear() 或 viewWillLayoutSubviews() 设置角半径?
- roblox - Roblox - 虽然测试桁架在我将它们拖入时工作,但是当我使用脚本设置所有东西时,我无法爬上它
- scala - 带有 java 库的 Scala gradle 控制台应用程序