为什么在向Django应用程序添加rest\u框架时会出现ModuleNotFoundError

2024-09-28 16:52:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个新的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'

发生了什么事


Tags: djangoinpylibpackageslocallinesite
1条回答
网友
1楼 · 发布于 2024-09-28 16:52:03

使用检查虚拟环境中安装的软件包

$ pip freeze

如果在列表中找不到“djangorestframework”软件包,那么它没有安装,请通过$ pip install djangorestframework 安装django rest framework

看起来您正在虚拟环境中使用pipenv

使用激活项目文件夹中的虚拟环境

$ pipenv shell

并安装所需的软件包,即django、djangorestframework等。。像

$ pipenv install djangorestframework

如果您正在使用pipenv,请使用$ pipenv install {package-name}

相关问题 更多 >