Drf-yasg: No module named '_contextVars'

Created on 7 May 2019  路  11Comments  路  Source: axnsan12/drf-yasg

Hi,
I am getting the following error :

Error:
ImportError: Could not import 'drf_yasg.generators.OpenAPISchemaGenerator' for API setting 'DEFAULT_GENERATOR_CLASS'. ModuleNotFoundError: No module named '_contextvars'.

OS:
Windows 10

Python:
3.7.2

Installed libraries:
certifi==2019.3.9
chardet==3.0.4
coreapi==2.3.3
coreschema==0.0.4
Django==2.1.7
django-rest-swagger==2.2.0
djangorestframework==3.9.2
drf-yasg==1.15.0
humanfriendly==4.18
idna==2.8
inflection==0.3.1
itypes==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
openapi-codec==1.3.2
psycopg2-binary==2.8.1
pyreadline==2.1
pytz==2018.9
requests==2.21.0
ruamel.yaml==0.15.94
simplejson==3.16.0
six==1.12.0
uritemplate==3.0.0
urllib3==1.24.3

urls.py

from django.urls import path, include
from . import views
from rest_framework import routers

# drf-yasg

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
    openapi.Info(
        title="Snippets API",
        default_version='v1',
        description="Test description",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="[email protected]"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

router = routers.DefaultRouter()
router.register('', views.DefaultQView)

urlpatterns = [
    path('', include(router.urls)),
    path('docs/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),

]

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Local
    'languages.apps.LanguagesConfig',
    'api.apps.ApiConfig',
    'dashboard.apps.DashboardConfig',
    # 3rd party
    'rest_framework',
    'django.contrib.humanize',
    'drf_yasg',

]

Full Stacktrace:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x045D1930>
Traceback (most recent call last):
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\rest_framework\settings.py", line 183, in import_from_string
    module = import_module(module_path)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\drf_yasg\generators.py", line 18, in <module>
    from .inspectors.field import get_basic_type_info, get_queryset_field, get_queryset_from_view
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\drf_yasg\inspectors\__init__.py", line 10, in <module>
    from .query import CoreAPICompatInspector, DjangoRestResponsePagination
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\drf_yasg\inspectors\query.py", line 3, in <module>
    import coreschema
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\coreschema\__init__.py", line 6, in <module>
    from coreschema.encodings.html import render_to_form
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\coreschema\encodings\html.py", line 2, in <module>
    import jinja2
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\jinja2\__init__.py", line 82, in <module>
    _patch_async()
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\jinja2\__init__.py", line 78, in _patch_async
    from jinja2.asyncsupport import patch_all
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\jinja2\asyncsupport.py", line 13, in <module>
    import asyncio
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37-32\Lib\asyncio\__init__.py", line 8, in <module>
    from .base_events import *
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37-32\Lib\asyncio\base_events.py", line 39, in <module>
    from . import events
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37-32\Lib\asyncio\events.py", line 14, in <module>
    import contextvars
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37-32\Lib\contextvars.py", line 1, in <module>
    from _contextvars import Context, ContextVar, Token, copy_context
ModuleNotFoundError: No module named '_contextvars'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\core\management\base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\urls\resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\urls\resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\UserR\PycharmProjects\DefaultFramework\DefaultFramework\urls.py", line 7, in <module>
    path('api/v1/', include("api.urls")),
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\UserR\PycharmProjects\DefaultFramework\api\urls.py", line 21, in <module>
    permission_classes=(permissions.AllowAny,),
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\drf_yasg\views.py", line 68, in get_schema_view
    _generator_class = generator_class or swagger_settings.DEFAULT_GENERATOR_CLASS
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\drf_yasg\app_settings.py", line 121, in __getattr__
    val = perform_import(val, attr)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\rest_framework\settings.py", line 170, in perform_import
    return import_from_string(val, setting_name)
  File "C:\Users\UserR\.virtualenvs\DefaultFramework-6tISKjYo\lib\site-packages\rest_framework\settings.py", line 187, in import_from_string
    raise ImportError(msg)
ImportError: Could not import 'drf_yasg.generators.OpenAPISchemaGenerator' for API setting 'DEFAULT_GENERATOR_CLASS'. ModuleNotFoundError: No module named '_contextvars'.

invalid

Most helpful comment

Hi! In my case problem was with virtualenv. When I updated python to 3.7.3 for 64bit OS Windows it didn't update python in virtualenv. In command prompt showed Python 3.7.3, in virtualenv python 3.7.2 for 32bit OS. Every time I ran Flask App I met ImportError(_contextvars didn't found). After I had reinstalled virtualenv through new version in the same directory (in order to save dependencies) this error disappeared.

All 11 comments

I have met the same issue after updated python to v3.7.3 and can't fix this.

I solved it. In my case virtualenv didn't update py version. After I did it manually issue disappeared!

Seems to be a python distribution issue and not really related to drf-yasg - contextvars is a builtin module.

I am still facing this problem in my flask app and can't seem to solve it for about a week now. Please @AleksMD how did you solve this issue?

Same!! please help!

I have this problem in my pyramid app - should i just revert the python version back ?

it's interesting the issue is so unsolvable....by even the pros. Googled everywhere and no one's got the answer...

Hi! In my case problem was with virtualenv. When I updated python to 3.7.3 for 64bit OS Windows it didn't update python in virtualenv. In command prompt showed Python 3.7.3, in virtualenv python 3.7.2 for 32bit OS. Every time I ran Flask App I met ImportError(_contextvars didn't found). After I had reinstalled virtualenv through new version in the same directory (in order to save dependencies) this error disappeared.

Also I deleted old version from PATH. Hope all of the above will help you. Wish you great coding)

For me the issue was I was using a stale virtualenv created with python 3.7.2 while my root python has been updated to 3.7.4.
Purging and recreating the venv solved it.

Thanks @AleksMD @refack . Your solutions gave me the way out. So I purged my Virtualenv and re-install with updated Python 3.7. Now it works perfectly. @sarahjeeeze You should do same.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamhssingh picture iamhssingh  路  3Comments

mkurnikov picture mkurnikov  路  3Comments

hnykda picture hnykda  路  3Comments

beaugunderson picture beaugunderson  路  5Comments

mkurnikov picture mkurnikov  路  4Comments