简体   繁体   English

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

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

I got an error,ModuleNotFoundError: No module named 'rest_framework' when I run command python manage.py runserver .运行命令python manage.py runserver时出现错误,ModuleNotFoundError: No module named 'rest_framework'。 Traceback says追溯 说

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'

I already run command pip3 install djangorestframework ,when I run this command again, Requirement already satisfied: djangorestframework in /usr/local/lib/python3.6/site-packages shows in terminal.Python version is 3.6.2.What is wrong in my code?How should I fix this?I use Anaconda virtual environment.我已经运行命令pip3 install djangorestframework ,当我再次运行此命令时,要求已经满足:/usr/local/lib/python3.6/site-packages 中的 djangorestframework 显示在终端中。Python 版本是 3.6.2。出了什么问题我的代码?我应该如何解决这个问题?我使用 Anaconda 虚拟环境。

It looks as if pip3 install has installed the package into /usr/local/lib/python3.6/site-packages , instead of your environment /Users/xxx/anaconda/envs/env/lib/python3.6/site-packages .看起来pip3 install已将软件包安装到/usr/local/lib/python3.6/site-packages ,而不是您的环境/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages .

If you use python -m pip , then the package will be installed in the same version of python that you use when you run python manage.py runserver .如果您使用python -m pip ,那么该软件包将安装在您运行python manage.py runserver时使用的相同版本的 python 中。 This is the suggested command in the Python docs .这是Python 文档中建议的命令。

python -m pip install djangorestframework

Did you add rest_framework in your setup?您是否在设置中添加了 rest_framework?

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

May be this post will help you.也许这篇文章会对你有所帮助。 Have a look.看一看。

(depending on the version of python you are) (取决于你的python版本)

pip/pip3 install djangorestframework

This solved it for me.这为我解决了它。

First thing you need to check your project interpreter: if you are using Pycharm: you go to Pycharm -> preferences/settings -> project interpreter and check if it's there among other installed libraries?首先你需要检查你的项目解释器:如果你使用 Pycharm:你去 Pycharm -> 首选项/设置 -> 项目解释器并检查它是否在其他已安装的库中?

if it's not you need to add it manually.如果不是,您需要手动添加它。 To do so there is + button where you can add a new package, search for " djangorestframework " and download it.为此,您可以使用 + 按钮添加新包,搜索“ djangorestframework ”并下载它。 restart the server and you are good to go重新启动服务器,您就可以开始了

This command python -m pip install djangorestframework says that djangorestframework is already installed .这个命令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)

Then I tried this python3 -m pip install djangorestframework which started the installation .然后我尝试了这个开始安装的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

After this, I was able to run python3 manage.py makemigrations在此之后,我能够运行python3 manage.py makemigrations

Add the module you intend to use in your django project by telling the django to collect it from lib by including them in your settings file something like this添加您打算在 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.
]

But let me tell you one of the best practices I follow to keep it neat.但是让我告诉你我遵循的保持整洁的最佳实践之一。

I create another list called THIRD_PARTY_LIBS which I'll concatenate to the original INSTALLED_APPS something like this我创建了另一个名为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.. 

Thank you!谢谢!

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

exiting virtual environment and then restarting solved the issue for me.退出虚拟环境然后重新启动为我解决了这个问题。 First run pip freeze in your virtual environment to confirm that the pip install djangorestframework did actually install.首先在您的虚拟环境中运行pip freeze以确认pip install djangorestframework确实安装了。

If you see it listed after running pip freeze , then try exiting the virtual environment and then reactivating the virtual environment and then running server again etc.如果您在运行pip freeze后看到它列出,请尝试退出虚拟环境,然后重新激活虚拟环境,然后再次运行服务器等。

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

暂无
暂无

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

相关问题 ModuleNotFoundError:没有名为&#39;rest_framework&#39;的模块python3 django - ModuleNotFoundError: No module named 'rest_framework' python3 django 没有名为rest_framework的模块 - No module named rest_framework ModuleNotFoundError:运行 celery -A backend worker -l info 时没有名为“rest_framework”的模块 - ModuleNotFoundError: No module named 'rest_framework' when running celery -A backend worker -l info 没有名为rest_framework的模块。 Django + Docker - No module named rest_framework . Django + Docker 导入“rest_framework”无法解决。 但是我已经安装了djangorestframework,不知道怎么回事 - Import "rest_framework" could not be resolved. But I have installed djangorestframework, I don't know what is going wrong Django Rest 框架 -- 没有名为 rest_framework 的模块 - Django Rest Framework -- no module named rest_framework ModuleNotFoundError:没有名为“pandas”的模块 - 当我已经安装了熊猫时 - ModuleNotFoundError: No module named 'pandas' - when I have pandas already installed ModuleNotFoundError:没有名为“msgpack”的模块,但已经安装了 msgpack - ModuleNotFoundError: No module named 'msgpack', but msgpack is already installed ModuleNotFoundError:没有名为“六”的模块,六已安装 - ModuleNotFoundError: No module named 'six' , Six is already installed ModuleNotFoundError:没有名为“rest_framework.simplejwt”的模块 - ModuleNotFoundError: No module named 'rest_framework.simplejwt'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM