繁体   English   中英

ModuleNotFoundError: No module named 'rest_framework' 我已经安装了 djangorestframework

[英]ModuleNotFoundError: No module named 'rest_framework' I already installed djangorestframework

运行命令python manage.py runserver时出现错误,ModuleNotFoundError: No module named 'rest_framework'。 追溯 说

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x108193ae8>
Traceback (most recent call last):
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
    mod = import_module(mod_path)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'rest_framework'

我已经运行命令pip3 install djangorestframework ,当我再次运行此命令时,要求已经满足:/usr/local/lib/python3.6/site-packages 中的 djangorestframework 显示在终端中。Python 版本是 3.6.2。出了什么问题我的代码?我应该如何解决这个问题?我使用 Anaconda 虚拟环境。

看起来pip3 install已将软件包安装到/usr/local/lib/python3.6/site-packages ,而不是您的环境/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages .

如果您使用python -m pip ,那么该软件包将安装在您运行python manage.py runserver时使用的相同版本的 python 中。 这是Python 文档中建议的命令。

python -m pip install djangorestframework

您是否在设置中添加了 rest_framework?

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

也许这篇文章会对你有所帮助。 看一看。

(取决于你的python版本)

pip/pip3 install djangorestframework

这为我解决了它。

首先你需要检查你的项目解释器:如果你使用 Pycharm:你去 Pycharm -> 首选项/设置 -> 项目解释器并检查它是否在其他已安装的库中?

如果不是,您需要手动添加它。 为此,您可以使用 + 按钮添加新包,搜索“ djangorestframework ”并下载它。 重新启动服务器,您就可以开始了

这个命令python -m pip install djangorestframework表示djangorestframework 已经安装

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: djangorestframework in /Users/rana.singh/Library/Python/2.7/lib/python/site-packages (3.9.4)

然后我尝试了这个开始安装的python3 -m pip install djangorestframework

Collecting djangorestframework
  Downloading djangorestframework-3.11.0-py3-none-any.whl (911 kB)
     |████████████████████████████████| 911 kB 1.6 MB/s 
Requirement already satisfied: django>=1.11 in /usr/local/lib/python3.7/site-packages/Django-3.1-py3.7.egg (from djangorestframework) (3.1)
Requirement already satisfied: asgiref>=3.2 in /usr/local/lib/python3.7/site-packages/asgiref-3.2.7-py3.7.egg (from django>=1.11->djangorestframework) (3.2.7)
Requirement already satisfied: pytz in /usr/local/lib/python3.7/site-packages/pytz-2020.1-py3.7.egg (from django>=1.11->djangorestframework) (2020.1)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.7/site-packages/sqlparse-0.3.1-py3.7.egg (from django>=1.11->djangorestframework) (0.3.1)
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.11.0

在此之后,我能够运行python3 manage.py makemigrations

添加您打算在 django 项目中使用的模块,方法是告诉 django 从 lib 中收集它,方法是将它们包含在您的设置文件中,如下所示

INSTALLED_APPS = [

'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',                # now django knows you will use it in your project.
]

但是让我告诉你我遵循的保持整洁的最佳实践之一。

我创建了另一个名为THIRD_PARTY_LIBS列表,我将把它连接到原始的INSTALLED_APPS ,就像这样

THIRD_PARTY_APPS = ['rest_framework'] # this list shall contain many more of those useful apps and stuff.

INSTALLED_APPS += THIRD_PARTY_APPS  # Boom.. the things goes skraa.. pop.. pop.. 

谢谢!

如果您使用的是 pipenv shell,请确保使用 pipenv 重新安装“丢失的包”例如pipenv install djangorestframework这解决了我的问题

退出虚拟环境然后重新启动为我解决了这个问题。 首先在您的虚拟环境中运行pip freeze以确认pip install djangorestframework确实安装了。

如果您在运行pip freeze后看到它列出,请尝试退出虚拟环境,然后重新激活虚拟环境,然后再次运行服务器等。

首先,您必须通过 python manage.py 进行迁移,然后迁移

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM