【问题标题】:Pycharm with Django throws ImportError: cannot import name 'unittest'Pycharm 与 Django 抛出 ImportError: cannot import name 'unittest'
【发布时间】:2017-11-15 15:58:35
【问题描述】:

我正在学习 Django 教程,并试图让测试用例与 PyCharm 一起运行。然而我遇到了一个问题。当我运行命令时:app test

我遇到了这个异常:测试框架意外退出:

F:\programming\Python\python.exe "F:\programming\JetBrains\PyCharm 2017.2.4\helpers\pycharm\django_test_manage.py" test a F:\programming\Projects\pycharm\untitled
Testing started at 4:54 PM ...
Traceback (most recent call last):
  File "F:\programming\JetBrains\PyCharm 2017.2.4\helpers\pycharm\django_test_manage.py", line 157, in <module>
    utility.execute()
  File "F:\programming\JetBrains\PyCharm 2017.2.4\helpers\pycharm\django_test_manage.py", line 110, in execute
    from django_test_runner import is_nosetest
  File "F:\programming\JetBrains\PyCharm 2017.2.4\helpers\pycharm\django_test_runner.py", line 42, in <module>
    from django.utils import unittest
ImportError: cannot import name 'unittest'

Process finished with exit code 1

显然,django_test_manage.py 文件不起作用。我怎样才能解决这个问题? 即使test.py 类为空,也会发生这种情况。所以这肯定是 PyCharm 的问题 then(?) 我正在使用 PyCharm Pro 2017.2.4、Django 2.0 和 Python 3.6 我的运行/调试配置只是 PyCharm 所做的基本的预设 Django 设置。

【问题讨论】:

  • 我想知道您是否查看了错误版本的教程?这是 django 2.0 的版本docs.djangoproject.com/en/2.0/intro/tutorial05
  • 不,这是正确的。但不管我的测试用例如何,我根本无法运行测试(即使 tests.py 类为空,我也会遇到同样的错误)
  • 安装了我在 2017 版本上的 2018.2 版本,它无法为我导入项目中的子模块 - python 2.7 。只是调试器造成了痛苦。

标签: django pycharm django-testing


【解决方案1】:

新的 PyCharm 修复了这个错误。

如果更新 PyCharm 不是一个选项,您可以在 django_test_runner.py 上更改以下行:

 if VERSION[1] >= 7:
   import unittest
 else:
   from django.utils import unittest

到:

 if VERSION >= (1, 7):
   import unittest
 else:
   from django.utils import unittest

再次在第 56 行更改为:

if VERSION >= (1, 6):

最后在第 256 行更改为:

if VERSION >= (1, 1):

为什么?因为VERSION指的是django的版本,已经打勾到2点东西。

【讨论】:

  • 文件中还有if VERSION[1] &gt;= 6:,应该用if VERSION &gt;= (1, 6):替换,if VERSION[1] &gt;= 1:应该用if VERSION &gt;= (1, 1):替换,一切都像魅力一样。
【解决方案2】:

django.utils.unittest 在 Django 1.9 中被删除了所以我怀疑你可能使用的是旧版本的教程。

在 pycharm 中,您使用的是 django.tests.testcases 运行配置吗?最好使用Python unittest.TestCase 详细here

编辑:所以在django_test_runner.py 你有以下内容:

from django.test.testcases import TestCase
from django import VERSION

# See: https://docs.djangoproject.com/en/1.8/releases/1.7/#django-utils-unittest
# django.utils.unittest provided uniform access to the unittest2 library on all Python versions.
# Since unittest2 became the standard library's unittest module in Python 2.7,
# and Django 1.7 drops support for older Python versions, this module isn't useful anymore.
# It has been deprecated. Use unittest instead.
if VERSION >= (1,7):
  import unittest
else:
  from django.utils import unittest

因此,您在解释器中用于测试 runco​​nfig 的 django 版本似乎 django.utils.unittest 时。)如果您执行 from django import VERSION 并在解释器中打印它会返回什么?

【讨论】:

  • 我没有调用 unittest,pycharm 在它的 django_test_runner.py 类中
  • 你还能指出它说单元测试被删除的地方吗?它在这里说它们是首选方式:docs.djangoproject.com/en/2.0/topics/testing
  • code.djangoproject.com/ticket/20680 它推荐的 unittest 库是 PYTHON 标准库 unittest (请参阅您发布的页面上的链接)而不是内置的 django.utils.unittest 库,它是一个副本并已被删除
  • 使用 unittest.TestCase 而不是 Django.test 测试用例仍然会抛出同样的错误。
  • 我在 django 2.0rc1 中使用 virtualenv
【解决方案3】:

我认为这是 Pycharm 的 django_test_runner.py 中的一个错误。

在我的pycharm中,代码是:

  if VERSION[1] >= 7:
    import unittest
  else:
    from django.utils import unittest

但是你(和我)使用 Django 2.0,所以 pycharm import 'from django.utils import unittest' ...

我已经像这样修改了我的 test_runner:

  if VERSION[0] > 1 or VERSION[1] >= 7:
    import unittest
  else:
    from django.utils import unittest

你需要在其他地方用相同的技巧修改相同的文件。

这是工作!

【讨论】:

  • 更好的方法是if VERSION &gt;= (1, 7)。因为您的代码会为 1.7 以下的 django 错误地导入。
  • ModuleNotFoundError: No module named 'django.test.simple'
【解决方案4】:

django_test_runner.py 实际上存在更多问题。有用的是用这个版本替换它:https://gist.github.com/IlianIliev/6f884f237ab52d10aa0e22d53df97141

可以在&lt;pycharm_root&gt;/helpers/pycharm找到

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 2018-10-27
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多