pythondjangovirtualenvpython-3.7python-contextvars

No module named '_contextvars' in Python 3.7.3 virtual environment


I'm working on a Django project which requires Python3.7.3 virtual environment on Ubuntu 16. So I created a virtual environment and installed all the requirements in it and verified it, activated it.

But when I try to run the Django server using runserver it is giving me below error.

Traceback (most recent call last):
  File "/usr/lib/python3.7/decimal.py", line 3, in <module>
    from _decimal import *
ModuleNotFoundError: No module named '_decimal'

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "./interfaces/control/manage.py", line 8, in <module>
        execute_from_command_line(sys.argv)
      File "/home/path/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
        utility.execute()
      File "/home/path/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/home/path/lib/python3.7/site-packages/django/core/management/__init__.py", line 224, in fetch_command
        klass = load_command_class(app_name, subcommand)
      File "/home/path/lib/python3.7/site-packages/django/core/management/__init__.py", line 36, in load_command_class
        module = import_module('%s.management.commands.%s' % (app_name, name))
      File "/home/path/lib/python3.7/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 "/home/path/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 10, in <module>
        from django.core.servers.basehttp import (
      File "/home/path/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 17, in <module>
        from django.core.handlers.wsgi import LimitedStream
      File "/home/path/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 8, in <module>
        from django.core.handlers import base
      File "/home/path/lib/python3.7/site-packages/django/core/handlers/base.py", line 7, in <module>
        from django.urls import get_resolver, set_urlconf
      File "/home/path/lib/python3.7/site-packages/django/urls/__init__.py", line 1, in <module>
        from .base import (
      File "/home/path/lib/python3.7/site-packages/django/urls/base.py", line 4, in <module>
        from django.utils.encoding import iri_to_uri
      File "/home/path/lib/python3.7/site-packages/django/utils/encoding.py", line 4, in <module>
        from decimal import Decimal
      File "/usr/lib/python3.7/decimal.py", line 8, in <module>
        from _pydecimal import *
      File "/usr/lib/python3.7/_pydecimal.py", line 436, in <module>
        import contextvars
      File "/usr/lib/python3.7/contextvars.py", line 1, in <module>
        from _contextvars import Context, ContextVar, Token, copy_context
    ModuleNotFoundError: No module named '_contextvars'

Note that it is first going to Django project in my virtualenv here.

/home/path/lib/python3.7/site-packages/django/core/management/__init__.py

Again it is searching in the global location of python here.

/usr/lib/python3.7/_pydecimal.py

Can someone please let me know why it is going to the global location and throwing the error.


Solution

  • The issue was with Global Python interpreter itself.

    I followed Installing Python 3.7 on Ubuntu from Source in https://linuxize.com/post/how-to-install-python-3-7-on-ubuntu-18-04/ for installing Python 3.7.

    Somehow it is not installing all the required packages for python interpreter.

    Everything works fine if I install using Installing Python 3.7 on Ubuntu with Apt.