简体   繁体   English

导入“rest_framework”无法解决。 但是我已经安装了djangorestframework,不知道怎么回事

[英]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.这对我有用。

  1. Run in terminal在终端运行

    pip install django-rest-framework
  2. Add 'rest_framework' to INSTALLED APPS in settings.pysettings.py 'rest_framework'添加到 INSTALLED APPS

  3. If it does not work, restart the editor(vscode or something)如果还是不行,重启编辑器(vscode什么的)

  1. Control+ shift + p.控制+移位+ p。
  2. type ' Python: Select Interpreter ' and select the same. type ' Python: Select Interpreter ' 和 select 一样。
  3. choose your virtual env from the list if it is not listed please choose Enter Interpreter path'如果未列出,请从列表中选择您的虚拟环境,请选择Enter Interpreter path'
  4. Give pathe like this 'c:\users\user\django\myvenv\scripts\python.ex'像这样给出路径'c:\users\user\django\myvenv\scripts\python.ex'

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/pip name_of_virtualenv/脚本/pip

If shown otherwise, to solve this issue:如果另有显示,要解决此问题:

  1. delete the virtual environment folder and create it again.删除虚拟环境文件夹并重新创建。
  2. which pip must show the right path now in the new virtual env terminal. which pip现在必须在新的虚拟环境终端中显示正确的路径。
  3. reinstall the packages.重新安装软件包。

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.我遇到了类似的问题。

  • go to your venv folder > Lib go 到您的 venv 文件夹 > Lib
  • make sure you see the djangorestframework and rest_framework folder确保您看到 djangorestframework 和 rest_framework 文件夹
  • if they are absent, you should know you are pip installing those packages in the wrong venv.如果它们不存在,您应该知道您是 pip 将这些软件包安装在错误的 venv 中。

Answers of @nayburz and @Faseela worked for me. @nayburz 和 @Faseela 的答案对我有用。

  1. Control+ shift + p.控制+移位+ p。
  2. type 'Python: Select Interpreter' and select the same.键入“Python:Select 解释器”和 select 相同。
  3. choose your virtual env from the list if it is not listed please choose Enter Interpreter path'如果未列出,请从列表中选择您的虚拟环境,请选择 Enter Interpreter path'
  4. path of your virtual env python.exe file.您的虚拟环境 python.exe 文件的路径。

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。

  1. open Terminal打开终端
  2. navigate to your project folder.导航到您的项目文件夹。
  3. activate virtual environment ( > source {path_to_venv_home}\bin\activate)激活虚拟环境(> source {path_to_venv_home}\bin\activate)
  4. now open vscode with 'code.'现在用'code'打开vscode。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM