首页 > 解决方案 > Python Django 错误 ModuleNotFoundError:没有名为“corsheaders”的模块

问题描述

嘿伙计们,当我安装了 corsheaders 时,我收到了以下错误,任何想法为什么我会收到以下错误消息,因为我没有想法。

installed:
Python 3.7.2

certifi==2018.11.29

chardet==3.0.4

Django==2.1.7

django-cors-headers==2.4.0

django-mssql==1.8

django-oauth-toolkit==1.2.0

djangorestframework==3.9.1

idna==2.8

oauthlib==3.0.1

pytz==2018.9

requests==2.21.0

urllib3==1.24.1

(Oauth) C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth>Scripts\python.exe mysite\manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03AC8C48>
Traceback (most recent call last):
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\lib\site-packages\django\apps\registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Users\michael.mountford\OneDrive\Programming\matt-mikes-practice\Oauth\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 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'corsheaders'

标签: pythondjangopython-3.xoauthdjango-cors-headers

解决方案


我有同样的问题。如果“corsheaders”在您的 INSTALLED_APPS 中并且“corsheaders.middleware.CorsMiddleware”在您的 MIDDLEWARE 中,那么需要检查的是您的虚拟环境。

进入 /venv 文件夹并在 /site-packages 中查找“corsheaders”文件夹。

在我的情况下,我复制了我的项目文件夹,以便在我这样做时pip install django-cors-headers将其安装在原始项目的 venv 中。这可能就是为 MichaelMountford 重新创建 venv 的原因。


推荐阅读