Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 447
fogx Avatar asked Nov 15 '17 15:11

fogx


2 Answers

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.

like image 114
thyago stall Avatar answered Oct 16 '22 15:10

thyago stall


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?

like image 30
ptr Avatar answered Oct 16 '22 14:10

ptr