簡體   English   中英

當我將 rest_framework 添加到我的 django 應用程序時,為什么會出現 ModuleNotFoundError

[英]Why am i getting a ModuleNotFoundError when i add rest_framework to my django app

我創建了一個新的 django 應用程序並使用 pipenv 版本 2020.11.15 安裝了 djangorestframework 並將其注冊到 settings.py 文件中

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'leads',
    'rest_framework'
]

我在運行python manage.py makemigrations時遇到此錯誤

C:\Users\eliHeist\Python Workspace\lead_manager_react_django\leadmanager>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\eliHeist\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 419, in
execute_from_command_line
    utility.execute()
  File "C:\Users\eliHeist\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 395, in
execute
    django.setup()
  File "C:\Users\eliHeist\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\eliHeist\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\eliHeist\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\config.py", line 224, in create
    import_module(entry)
  File "C:\Users\eliHeist\AppData\Local\Programs\Python\Python37\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 'rest_framework'

到底是怎么回事?

使用檢查安裝在虛擬環境中的軟件包

$ pip freeze

if you can't find the "djangorestframework" package in the list, then its not installed, install django rest framework by $ pip install djangorestframework

看起來您正在將pipenv用於虛擬環境,

使用激活項目文件夾中的虛擬環境

$ pipenv shell

並安裝所需的軟件包,即 django、djangorestframework 等。比如

$ pipenv install djangorestframework

如果您使用的是 pipenv,請使用$ pipenv install {package-name}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM