Python 3.7.3 虚拟环境中没有名为“_contextvars”的模块

No module named '_contextvars' in Python 3.7.3 virtual environment

我正在开发 Django 项目,该项目需要 Python3.7.3 虚拟环境 Ubuntu16。 所以我创建了一个虚拟环境并在其中安装了所有要求并验证了它,激活了它。

但是当我尝试使用 runserver 运行 Django 服务器时,出现以下错误。

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'

注意这里先去我的virtualenv中的Django项目

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

它再次在此处搜索 python 的全局位置。

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

谁能告诉我为什么它会转到全局位置并抛出错误。

问题出在全局 Python 解释器本身。

我按照 Installing Python 3.7 on Ubuntu from Source in https://linuxize.com/post/how-to-install-python-3-7-on-ubuntu-18-04/ 安装 Python 3.7.

不知何故,它没有为 python 解释器安装所有必需的包。

如果我使用 Installing Python 3.7 on Ubuntu with Apt.

安装一切正常

在我的例子中,安装在虚拟环境中的 python 版本是 3.7.2,而我电脑中唯一的版本是 3.7.3。所以,我重新安装了 3.7.2 版本,它成功了。 希望它对你有用!!!

对我来说,我检查了这个位置,发现我的 requirements.txt 以某种方式将 Werkzeug 更改为 1.5x 版本。所以我恢复了这些更改,解决了我的问题。

试试这个。

pip3 install Werkzeug==2.0.1