'Pycharm with Django throws ImportError: cannot import name 'unittest'

I am working through the Django tutorial, and am trying to get the test cases to run with PyCharm. I have encountered a problem however. When I run the command: app test

I am faced with this exception: test framework quit unexpectedly:

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

Apparently, the django_test_manage.py file does not work. How can I fix this? this happens even when the test.py class is empty. So it must be a problem with PyCharm then(?) I am using PyCharm Pro 2017.2.4, Django 2.0 and Python 3.6 My run/debug configurations are just the basic, preset Django Settings that PyCharm does.



Solution 1:[1]

django.utils.unittest was removed in Django 1.9 So I suspect you may be using an old version of the tutorial.

In pycharm are you using the django.tests.testcases run config? Better to use the Python unittest.TestCase as detailed here

edit: So in django_test_runner.py you have the following:

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

So it appears that the version of django that you are using in the intepreter for your test runconfig is < 1.7 (when django.utils.unittest was deprecated.) What is returned if you do from django import VERSION and print it in your interpreter?

Solution 2:[2]

I think it's a bug in django_test_runner.py of Pycharm.

In my pycharm, the code is :

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

But you (and i) use Django 2.0, so pycharm import 'from django.utils import unittest' ...

I have modified my test_runner like that :

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

You need modify the same file in other places with the same tricks.

It's work !

Solution 3:[3]

There is actually more problems with django_test_runner.py. What helped was to replace it with this version: https://gist.github.com/IlianIliev/6f884f237ab52d10aa0e22d53df97141

It can be found in <pycharm_root>/helpers/pycharm

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Michal Holub