Python Visual Studio 代码中的 Pylint“未解决的导入”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53939751/
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
Pylint "unresolved import" error in visual studio code
提问by jAC
I am using the following setup
我正在使用以下设置
- MacOS Mojave
- Python 3.7.1
- Visual Studio Code 1.30
- Pylint 2.2.2
- Django 2.1.4
- MacOS 莫哈韦沙漠
- 蟒蛇 3.7.1
- Visual Studio 代码 1.30
- 派林特 2.2.2
- Django 2.1.4
I want to use linting to make my life a bit easier in visual studio code however, every import i have states "unresolved import". Even on default django imports (i.e. from django.db import models).
我想使用 linting 让我在 Visual Studio 代码中的生活更轻松,但是,我的每个导入都声明“未解析的导入”。即使是默认的 django 导入(即从 django.db 导入模型)。
I presume it is because it is not seeing the virtual environment python files.
我认为这是因为它没有看到虚拟环境 python 文件。
Everything works just fine but but it's starting to get annoying.
一切正常,但它开始变得烦人。
The interpreter choices i have are all system versions of python. It does not seem to see my virtual environment python at all (it is not in the same directory as my workspace, so that part makes sense).
我选择的解释器都是 python 的系统版本。它似乎根本没有看到我的虚拟环境 python(它与我的工作区不在同一个目录中,所以这部分是有道理的)。
If i setup the python.PythonPath in the settings.json file, it just ignores it and does not list my virtual environment path as an option. I also tried setting it up in my global python settings but it also does not show up.
如果我在 settings.json 文件中设置了 python.PythonPath,它只会忽略它并且不会列出我的虚拟环境路径作为选项。我也尝试在我的全局 python 设置中设置它,但它也没有显示。
Has anyone run into this issue and know a quick fix to get it working?
有没有人遇到过这个问题并知道快速修复以使其正常工作?
Thanks, jAC
谢谢, jac
采纳答案by ruddra
In your workspace settings, you can set your python path like this:
在您的工作区设置中,您可以像这样设置您的 python 路径:
{
"python.pythonPath": "/path/to/your/venv/bin/python",
}
回答by Shinebayar G
Accepted answer won't fix error when importing own modules.
Use following setting in your workspace settings .vscode/settings.json
导入自己的模块时,接受的答案不会修复错误。在您的工作区设置中使用以下设置.vscode/settings.json
"python.autoComplete.extraPaths": ["./path-to-your-code"],
参考:https: //github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
回答by ted
回答by Tomasz Chudzik
This issue has already been opened on GitHub: https://github.com/Microsoft/vscode-python/issues/3840There are 2 very useful answers, by MagnuesBrzenk and SpenHouet.
此问题已在 GitHub 上打开:https: //github.com/Microsoft/vscode-python/issues/3840MagnusBrzenk 和 SpenHouet 提供了 2 个非常有用的答案。
The best solution for now is to create a .env file in your project root folder. Then add a PYTHONPATH to it like this:
目前最好的解决方案是在项目根文件夹中创建一个 .env 文件。然后像这样添加一个 PYTHONPATH 到它:
PYTHONPATH=YOUR/MODULES/PATH
and in your settings.json add
并在您的 settings.json 添加
"python.envFile": ".env"
回答by sps
If you have this code in your settings.json
file, delete it
如果您的settings.json
文件中有此代码,请将其删除
{
"python.jediEnabled": false
}
回答by nairb
I was able to resolved this by enabling jedi in .vscode\settings.json
我能够通过在 .vscode\settings.json 中启用绝地来解决这个问题
"python.jediEnabled": true
Reference from https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-456017675
参考来自https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-456017675
回答by Ali Hesari
When I > reload window
that fixes.
当我> reload window
修复。
ref: https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-452657892
参考:https: //github.com/Microsoft/vscode-python/issues/3840#issuecomment-452657892
回答by David
If you are more visual like myself, you can use the VSCode configurations in File->Preferences->Settings(Ctrl+,). Go to Extensions->Python.
如果你更像我自己,你可以使用File->Preferences->Settings( Ctrl+,) 中的 VSCode 配置。转到扩展-> Python。
In the section Analysis: Disabled, add the suppression of the following message: unresolved-import
:
在Analysis: Disabled部分,添加对以下消息的抑制unresolved-import
:
回答by Lincoln
The solution from @Shinebayar G worked but this other one is a little bit more elegant:
@Shinebayar G 的解决方案有效,但另一个更优雅:
Copied from https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-463789294:
复制自https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-463789294:
Given the following example project structure:
给定以下示例项目结构:
- workspaceRootFolder
- .vscode
- ... other folders
- codeFolder
- 工作区根文件夹
- .vscode
- ... 其他文件夹
- 代码文件夹
What I did to resolve this issue:
我为解决此问题所做的工作:
- Go into the workspace folder (here workspaceRootFolder) and create a .env file
- In this empty .env file add the line PYTHONPATH=codeFolder (replace codeFolder with your folder name)
- Add "python.envFile": "${workspaceFolder}/.env" to the settings.json
- Restart VS code
- 进入工作区文件夹(这里是 workspaceRootFolder)并创建一个 .env 文件
- 在这个空的 .env 文件中添加行 PYTHONPATH=codeFolder (用您的文件夹名称替换 codeFolder)
- 将 "python.envFile": "${workspaceFolder}/.env" 添加到 settings.json
- 重启VS代码