Python Django Rest Framework -- 没有名为 rest_framework 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33308781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:10:22  来源:igfitidea点击:

Django Rest Framework -- no module named rest_framework

pythondjangopython-3.xpipdjango-rest-framework

提问by tryingtolearn

I've installed django rest framework using pip install djangorestframeworkyet I still get this error when I run "python3 manage.py sycndb":

我已经使用安装了 django rest 框架,pip install djangorestframework但是当我运行“python3 manage.py sycndb”时仍然出现此错误:

ImportError: No module named 'rest_framework'

导入错误:没有名为“rest_framework”的模块

I'm using python3, is this my issue?

我正在使用 python3,这是我的问题吗?

采纳答案by Vingtoft

You need to install django rest framework using pip3 (pip for python 3):

您需要使用 pip3(python 3 的 pip)安装 django rest 框架:

pip3 install djangorestframework

Instructions on how to install pip3 can be found here

可以在此处找到有关如何安装 pip3 的说明

回答by Burger King

Maybe you install DRF is for python2, not for python3.

也许你安装的 DRF 是针对 python2 的,而不是针对 python3 的。

You can use python consoleto check your module:

您可以使用python console来检查您的模块:

import rest_framework

Actually you use pipto install module, it will install python2 module.

实际上你pip用来安装模块,它会安装python2模块。

You should install the pip for python3:

您应该为python3安装pip:

sudo apt-get install python3-setuptools
sudo easy_install3 pip

So, you can install python3 module.

所以,你可以安装 python3 模块。

回答by Oleg G

When using a virtual environment like virtualenvwithout having django-rest-framework installed globally you might as well have the error. The solution would be:

当使用虚拟环境时,例如virtualenv没有全局安装 django-rest-framework,您可能会遇到错误。解决方案是:

  • activate the environment first with {{your environment name}}/bin/activatefor Linux or {{your environment name}}/Scripts/activatefor Windows

  • and then run the command again.

  • 首先{{your environment name}}/bin/activate为 Linux 或{{your environment name}}/Scripts/activateWindows激活环境

  • 然后再次运行该命令。

回答by Akshay Kumbhar

try this if you are using JWT pip install djangorestframework-jwt

如果您使用的是 JWT,请尝试此操作 pip install djangorestframework-jwt

回答by rpstw

if you forget ,,this will happen,it's weird

如果你忘记了,,这会发生,这很奇怪

wrong example: need a ,

错误的例子:需要一个 ,

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

回答by Vikhyath Rai

rest_framework module

rest_framework 模块

Try pressing the icon given if you are getting the following error:

如果出现以下错误,请尝试按给出的图标:

"ModuleNotFoundError: No module named 'rest_framework'"

“ModuleNotFoundError:没有名为‘rest_framework’的模块”

It will ask you to import the given package, that is rest_frameworkand run the code again.

它会要求您导入给定的包,即rest_framework再次运行代码。

This worked for me and trying to solve that error for a couple of days.

这对我有用,并试图解决该错误几天。

回答by O. Edholm

If you're using some sort of virtual environment do this!

如果您正在使用某种虚拟环境,请执行此操作!

  1. Exit from your virtual environment.

  2. Activate your virtual environment.

  1. 退出您的虚拟环境。

  2. 激活您的虚拟环境。

After you've done this you can try running your command again and this time it probably won't have any ImportErrors.

完成此操作后,您可以尝试再次运行您的命令,这次它可能不会有任何导入错误。

回答by Anna M

If you are working with PyCharm, I found that restarting the program and closing all prompts after adding 'rest_framework' to my INSTALLED_APPSworked for me.

如果您正在使用 PyCharm,我发现在将“rest_framework”添加到我INSTALLED_APPS为我工作后重新启动程序并关闭所有提示。

回答by martin-martin

Also, check for the possibility of a tiny typo:

另外,请检查是否有可能出现微小的错字

It's rest_frameworkwith an underscore(_) in between!

这是rest_framework下划线_)之间!

Took me a while to figure out that I was using a dashinstead...

我花了一段时间才发现我使用的是破折号......

回答by ggcarmi

if you used pipenv:

如果您使用pipenv

if you installed rest_framework thru the new pipenv, you need to run it thru the virtual environment:

如果您通过新的pipenv安装了rest_framework,则需要通过虚拟环境运行它:

1.pipenv shell

1. pipenv壳

2.(env) now, run your command(for example python manage.py runserver)

2.(env) 现在,运行你的命令(例如 python manage.py runserver)