[英]Import "rest_framework" could not be resolved. But I have installed djangorestframework, I don't know what is going wrong
Here's my settings.py:这是我的settings.py:
INSTALLED_APPS = [
'rest_framework',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api.apps.ApiConfig'
]
If you are using VSCode, Ctrl + Shift + P -> Type and select 'Python: Select Interpreter' and enter into your projects virtual environment.如果你使用 VSCode,Ctrl + Shift + P -> 输入 select 'Python: Select Interpreter'并进入你的项目虚拟环境。 This is what worked for me.
这对我有用。
Run in terminal在终端运行
pip install django-rest-framework
Add 'rest_framework'
to INSTALLED APPS in settings.py
在
settings.py
'rest_framework'
添加到 INSTALLED APPS
If it does not work, restart the editor(vscode or something)如果还是不行,重启编辑器(vscode什么的)
What if you selected the right interpreter (that of virtual environment), but still getting that error?如果您选择了正确的解释器(虚拟环境的解释器),但仍然出现错误怎么办?
Then first check the path of your pip in the terminal of that virtual environment.然后首先在该虚拟环境的终端中检查您的 pip 的路径。
Use this command: which pip
使用这个命令:
which pip
It should only point to the path where your virtual environment folder is located, for example:它应该只指向您的虚拟环境文件夹所在的路径,例如:
name_of_virtualenv/bin/pip
name_of_virtualenv/bin/pip
name_of_virtualenv/Scripts/pipname_of_virtualenv/脚本/pip
If shown otherwise, to solve this issue:如果另有显示,要解决此问题:
which pip
must show the right path now in the new virtual env terminal. which pip
现在必须在新的虚拟环境终端中显示正确的路径。Add your python file like C:\Users\hendrialqory\AppData\Local\Programs\Python\Python39-32\Lib\site-packages, Go to Setting Environment Variables and input your file python. Add your python file like C:\Users\hendrialqory\AppData\Local\Programs\Python\Python39-32\Lib\site-packages, Go to Setting Environment Variables and input your file python.
I was having similar problem.我遇到了类似的问题。
Answers of @nayburz and @Faseela worked for me. @nayburz 和 @Faseela 的答案对我有用。
mine was: D:\Python\Python_Django\trydjango\env\Scripts\python.exe我的是:D:\Python\Python_Django\trydjango\env\Scripts\python.exe
You can find similar path according to your project您可以根据您的项目找到类似的路径
for others using venv and if doing 'Python:Selecting Interpreter' and selecting your venv doesn't work then try these steps.对于使用 venv 的其他人,如果执行“Python:选择解释器”并选择您的 venv 不起作用,请尝试这些步骤。 These are for Mac.
这些适用于 Mac。
In case 'code.'如果是“代码”。 doesn't work then should add to your path.
不起作用,那么应该添加到您的路径中。 Try 'Launching from the command Line' section from https://code.visualstudio.com/docs/setup/mac
尝试https://code.visualstudio.com/docs/setup/mac中的“从命令行启动”部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.