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

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?


The new PyCharm has this bug fixed.

If update PyCharm is not an option, you can change the following lines on django_test_runner.py:

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

to:

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

Again at line 56 change to:

if VERSION >= (1, 6):

And finally at line 256 change to:

if VERSION >= (1, 1):

Why? Because the VERSION refers to the version of django, which has ticked over to 2 point something.


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 !