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
Django Rest Framework -- no module named rest_framework
提问by tryingtolearn
I've installed django rest framework using pip install djangorestframework
yet 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
回答by Burger King
Maybe you install DRF is for python2, not for python3.
也许你安装的 DRF 是针对 python2 的,而不是针对 python3 的。
You can use python console
to check your module:
您可以使用python console
来检查您的模块:
import rest_framework
Actually you use pip
to 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 virtualenv
without 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/activate
for Linux or{{your environment name}}/Scripts/activate
for Windowsand then run the command again.
首先
{{your environment name}}/bin/activate
为 Linux 或{{your environment name}}/Scripts/activate
Windows激活环境然后再次运行该命令。
回答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
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_framework
and 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!
如果您正在使用某种虚拟环境,请执行此操作!
Exit from your virtual environment.
Activate your virtual environment.
退出您的虚拟环境。
激活您的虚拟环境。
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_APPS
worked for me.
如果您正在使用 PyCharm,我发现在将“rest_framework”添加到我INSTALLED_APPS
为我工作后重新启动程序并关闭所有提示。
回答by martin-martin
Also, check for the possibility of a tiny typo:
另外,请检查是否有可能出现微小的错字:
It's rest_framework
with 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)