简体   繁体   English

Visual Studio 代码未解析导入?

[英]visual studio code unresolved import?

I have installed python version 3.7 and visual studio code latest version, after that i have created virtual environment and installed django and created Django project.我已经安装了python 3.7版和visual studio code最新版本,之后我创建了虚拟环境并安装了django并创建了Django项目。

Whenever i am opening some file, its showing error like below每当我打开某个文件时,它都会显示如下错误

unresolved import 'django.contrib' unresolved import 'django.urls' Undefined variable: 'path' Undefined variable: 'admin'未解析的导入 'django.contrib' 未解析的导入 'django.urls' 未定义变量:'path' 未定义变量:'admin'

Below are the paths of my application以下是我的应用程序的路径

1) Python : C:\\Python\\Python37-32\\python.exe 2) Virtual environment created D:\\django_projects\\envs\\py1\\ 3) Django Project created D:\\django_projects\\p1\\p1 1) Python : C:\\Python\\Python37-32\\python.exe 2) 虚拟环境创建 D:\\django_projects\\envs\\py1\\ 3) Django 项目创建 D:\\django_projects\\p1\\p1

Below are the things i have tried 1) Re Installing Python 2) Setting the Python Path in environment variable even i selected include in the path at the time of python installation 3) Re installing the VS Code 4) Tried commenting the "python.jediEnabled": false, in settings.json file in vs code, but it was giving different error unable to import django.以下是我尝试过的事情 1) 重新安装 Python 2) 在环境变量中设置 Python 路径,即使我在安装 python 时选择了包含在路径中 3) 重新安装 VS 代码 4) 尝试评论“python.jediEnabled “:false,在 vs 代码的 settings.json 文件中,但它给出了无法导入 django 的不同错误。 5) 5)

unresolved import 'django.contrib' unresolved import 'django.urls' Undefined variable: 'path' Undefined variable: 'admin'未解析的导入 'django.contrib' 未解析的导入 'django.urls' 未定义变量:'path' 未定义变量:'admin'

在此处输入图片说明

You did not select the virtual environment you installed Django into in VS Code (see the bottom-left corner for your screenshot where it says "Python 3.7.4 32-bit"; it would say "venv" or something if you were using a virtual environment).您没有选择在 VS Code 中安装 Django 的虚拟环境(请参阅屏幕截图的左下角,其中显示“Python 3.7.4 32 位”;如果您使用的是虚拟环境)。 Try clicking on the interpreter in the status bar and then select your environment .尝试单击状态栏中的解释器,然后选择您的环境

In my case, the error was:就我而言,错误是:

unresolved import 'pydotplus' Python (unresolved-import)

And it was not a 64 bit vs. 32 bit issue.这不是 64 位与 32 位的问题。 Instead, the wrong linting (because the code is running, and there is just wrong underlining in the editor) came from a needed extra python path in the json settings.相反,错误的 linting(因为代码正在运行,并且编辑器中有错误的下划线)来自 json 设置中需要的额外 python 路径。

At best, following https://github.com/Microsoft/python-language-server/issues/887 and there the approach of HozcarAndres and the one after it.充其量,遵循https://github.com/Microsoft/python-language-server/issues/887以及 HozcarAndres 及其之后的方法。

//"python.pythonPath": "C:/Users/Admin/Anaconda3/python.exe",
"python.autoComplete.extraPaths": [
    "C:/Users/Admin/Anaconda3/Lib/site-packages/",
    ... (you can add further pahts in this String array)
    ]

"python.pythonPath" is not needed because it is the already known default.不需要“python.pythonPath”,因为它是已知的默认值。

Or go to settings.json (Ctrl+Shift+P and search for it) and change it to或者去 settings.json(Ctrl+Shift+P 并搜索它)并将其更改为

{
    [many settings...],
    [previous last line],
    "python.pythonPath": "C:/Users/Admin/Anaconda3/**"
}

(or change the existing "python.pythonPath", though this is not there as a default) (或更改现有的“python.pythonPath”,尽管这不是默认设置)

Then, the packages like django which are only in the C:/Users/Admin/Anaconda3/Lib/site-packages/ will automatically be recognised by the linting, while as a default, the path is only C:/Users/Admin/Anaconda3/python.exe - not enough to "know" the site-packages.然后,像 django 这样只在C:/Users/Admin/Anaconda3/Lib/site-packages/会被 linting 自动识别,而默认情况下,路径只有C:/Users/Admin/Anaconda3/python.exe - 不足以“了解”站点包。 And you cannot make a list of paths here, as only a single string can be entered.并且您不能在此处列出路径列表,因为只能输入一个字符串。

In case the Python interpreter gets lost after this, you can newly assign the python interpreter.如果 Python 解释器在此之后丢失,您可以重新分配 Python 解释器。 Go to the left bottom in the blue line and choose "Python 3.7..." interpreter again.转到蓝线的左下角,再次选择“Python 3.7 ...”解释器。

Any more settings on the linting are here: https://code.visualstudio.com/docs/python/linting更多关于 linting 的设置在这里: https ://code.visualstudio.com/docs/python/linting

Use the following setting in your workspace settings .vscode/settings.json :在您的工作区设置.vscode/settings.json使用以下.vscode/settings.json

"python.autoComplete.extraPaths": ["./path-to-your-code"],

Or if you're using Pylance :或者,如果您使用的是Pylance

"python.analysis.extraPaths": ["./sources"]

Example例子

Consider the following directory考虑以下目录

.
├── vscode
│   └── settings.json
└── src
    ├── main.py
    └── assets
        └──module.py

settings.json would need to contain (If using Pylance) settings.json需要包含(如果使用 Pylance)

}
    "python.analysis.extraPaths": ["src/assets"]
}

And main.py needs something similar tomain.py需要类似于

from module import *

Reference: Troubleshooting, Unresolved import warnings参考: 故障排除,未解决的导入警告

On your Workspace vscode/setting.json simply place the "Python.autoComplete.extraPaths" to correspond to your Project File so that when you import a Module on your script, Your pyDev server can detect it and show no error to your Code.在您的工作区 vscode/setting.json 上,只需将“Python.autoComplete.extraPaths”放在与您的项目文件相对应的位置,这样当您在脚本中导入模块时,您的 pyDev 服务器可以检测到它并且不会显示您的代码错误。 eg "python.autoComplete.extraPaths": ["./path-to-your-script-code"],例如 "python.autoComplete.extraPaths": ["./path-to-your-script-code"],

在此处输入图片说明

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

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