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

Posted

技术标签:

【中文标题】ModuleNotFoundError: No module named \'rest_framework\' 我已经安装了 djangorestframework【英文标题】:ModuleNotFoundError: No module named 'rest_framework' I already installed djangorestframeworkModuleNotFoundError: No module named 'rest_framework' 我已经安装了 djangorestframework 【发布时间】:2018-06-12 22:35:55 【问题描述】:

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

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 虚拟环境。

【问题讨论】:

你在设置文件的installed_apps中添加了rest_framework吗 【参考方案1】:

看起来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 中。这是建议的命令in the Python docs。

python -m pip install djangorestframework

【讨论】:

【参考方案2】:

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

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

也许这个post 会帮助你。看看吧。

【讨论】:

rest_framework 已经在INSTALLED_APPS 中。报错是因为在INSTALLED_APPS中但是没有安装包。【参考方案3】:

(取决于你的python版本)

pip/pip3 install djangorestframework

这为我解决了。

【讨论】:

【参考方案4】:

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

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

【讨论】:

【参考方案5】:

这条命令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

【讨论】:

【参考方案6】:

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

【讨论】:

【参考方案7】:

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

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.. 

【讨论】:

rest_framework 已经在INSTALLED_APPS 中。报错是因为在INSTALLED_APPS中但是没有安装包。【参考方案8】:

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

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

【讨论】:

【参考方案9】:

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

【讨论】:

这对我有用!

以上是关于ModuleNotFoundError: No module named 'rest_framework' 我已经安装了 djangorestframework的主要内容,如果未能解决你的问题,请参考以下文章

ModuleNotFoundError: No module named 'gdbm'

Python:ModuleNotFoundError: No module named 'windows'

PySpark 自定义 UDF ModuleNotFoundError: No module named

用Java调用.py程序出现ModuleNotFoundError: No module named 'java'

ModuleNotFoundError: No module named 'jupyter_contrib_nbextensions' .ipynb文件转换.py文件时遇到错误(示例

ModuleNotFoundError: No module named 'jupyter_contrib_nbextensions' .ipynb文件转换.py文件时遇到错误(示例