PyCharm 中的 Django 测试

问题描述 投票:0回答:1

我有一个在 pycharm 中制作的简单 django 项目。目录结构如下:

zelda_botw_cooking_simulator
|-- cooking_simulator_project
|---- manage.py
|---- botw_cooking_simulator   # django app
|------ init.py
|------ logic.py
|------ tests.py
|------ all_ingredients.py
|------ other standard django app files
|---- cooking_simulator_project  # django project

当我在 PyCharm 终端中运行

python manage.py test
时,一切都运行良好。

但是,当我单击 PyCharm 中测试旁边的小三角形图标来运行该测试时,我收到一条错误消息:

File ".../zelda_botw_cooking_simulator/cooking_simulator_proj/botw_cooking_simulator/tests.py", line 5, in <module>
    from .all_ingredients import all_ingredients
ImportError: attempted relative import with no known parent package

我该如何解决这个问题?

我已经尝试在 PyCharm 中配置运行环境和测试环境 30 分钟,但我没有得到它。问题/答案herehere很接近,但没有足够的细节让我修复它。我到底应该在每个窗口的每个字段中放置什么? “目标”、“工作目录”是什么,我需要环境变量吗?设置部分包含哪些内容以及配置部分包含哪些内容? ChatGPT 推荐了一堆不起作用的东西。谢谢!

python django pycharm django-testing django-tests
1个回答
0
投票

这个错误只有2种可能:

  1. 要么没有环境变量,要么配置不正确
  2. all_ingredients 的路径是相对路径,尝试将其更改为绝对路径,将
    from .all_ingredients import all_ingredients
    更改为
    from botw_cooking_simulator.all_ingredients import all_ingredients

我非常愿意尝试检查第二个解决方案。您还可以检查错误:

File ".../zelda_botw_cooking_simulator/cooking_simulator_proj/botw_cooking_simulator/tests.py", line 5, in <module>
    from .all_ingredients import all_ingredients
ImportError: attempted relative import with no known parent package

这也指向同一件事:

ImportError: attempted relative import with no known parent package

© www.soinside.com 2019 - 2024. All rights reserved.