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

I am working on a django tutorial and I am trying to run test cases using pycharm. However, I ran into a problem. When I run the command: app test

I came across this exception: the test environment terminated 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 is not working. How can i fix this? this happens even when the test.py class is empty. So there must be a problem with pycharm then (?) I am using Pycharm Pro 2017.2.4, Django 2.0 and Python 3.6 My start / debug configurations are just the basic, pre-configured Django settings that pycharm does

Thank you!

+4
source share
4 answers

New PyCharm fixed this error.

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
+2

django.utils.unittest Django 1.9. , .

pycharm config django.tests.testcases run? Python unittest.TestCase

edit: , 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 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

, , django, intrereter runconfig, 1.7 ( django.utils.unittest .) , from django import VERSION ?

+6

, django_test_runner.py Pycharm.

pycharm :

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

( ) Django 2.0, pycharm django.utils import unittest '...

test_runner :

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

You need to change the same file in other places with the same tricks.

It works!

+2
source

Actually there are more problems with django_test_runner.py. What helped replace it with this version: https://gist.github.com/IlianIliev/6f884f237ab52d10aa0e22d53df97141

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

0
source

Source: https://habr.com/ru/post/1689326/


All Articles