可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
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).
I presume it is because it is not seeing the virtual environment python files.
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).
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.
Has anyone run into this issue and know a quick fix to get it working?
Thanks,
jAC
回答1:
In your workspace settings, you can set your python path like this:
{
"python.pythonPath": "/path/to/your/venv/bin/python",
}
回答2:
Accepted answer won't fix error when importing own modules.
Use following setting in your workspace settings .vscode/settings.json
"python.autoComplete.extraPaths": ["./path-to-your-code"],
ref: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
回答3:
This issue has already been opened on GitHub:
https://github.com/Microsoft/vscode-python/issues/3840
There are 2 very useful answers, by MagnuesBrzenk and SpenHouet.
The best solution for now is to create a .env file in your project root folder. Then add a PYTHONPATH to it like this:
PYTHONPATH=YOUR/MODULES/PATH
and in your settings.json add
"python.envFile": ".env"
回答4:
Alternative way: use the command interface!
cmd/ctrl + shift + p
> Python: Select Interpreter
> choose the one with the packages you look for
回答5:
If you have this code in your settings.json
file, delete it
{
"python.jediEnabled": false
}
回答6:
When I > reload window
that fixes.
ref: https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-452657892
回答7:
I was able to resolved this by enabling jedi in .vscode\settings.json
"python.jediEnabled": true
Reference from https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-456017675
回答8:
None of the above worked for me. Adding both of the lines below to my settings.json file did, however.
"python.analysis.disabled": [
"unresolved-import"
],
"python.linting.pylintArgs": ["--load-plugin","pylint_protobuf"]
The first line really just hides the linting error. Certainly not a permanent solution, but de-clutters the screen.
This answer gave me the second line: VS Code PyLint Error E0602 (undefined variable) with ProtoBuf compiled Python Structure
Maybe someone who understands python more than me can explain that one more.
回答9:
The solution from @Shinebayar G worked but this other one is a little bit more elegant:
Copied from https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-463789294:
Given the following example project structure:
- workspaceRootFolder
- .vscode
- ... other folders
- codeFolder
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
回答10:
I have a different solution: my VSCode instance had picked up the virtualenv stored in .venv
, but was using the wrong Python binary. It was using .venv/bin/python3.7
; using the switcher in the blue status bar, I changed it to use .venv/bin/python
and all of my imports were resolved correctly. I don't know what VSCode is doing behind the scenes when I do this, nor do I understand why this was causing my problem, but for me this was a slightly simpler solution than editing my workspace settings. I hope it helps someone.
回答11:
This works for me:
Open the command palette (Ctrl-Shift-P) and choose "Python: Select Interpreter".
Doing this you set the Python interpreter in VSCode
回答12:
My solution.
This solution is only for the current project.
- In the project root create folder
.vscode
- Then create the file
.vscode/settings.json
- in the file
setting.json
add the line (this is for python3)
{
"python.pythonPath": "/usr/local/bin/python3",
}
- This is the example for python 2
{
"python.pythonPath": "/usr/local/bin/python",
}
If you don't know where is located your python just run on the terminal the command which python
or which python3
in will print the python location.
This example works for dockerized Python - Django.
回答13:
Incase of pylint error install the following
pipenv install pylint-django
Then create a file .pylintrc in the root folder and write the following
load-plugins=pylint-django
回答14:
I have resolved import error by CTRL+Shift+P
type Preferences settings and select the option Preferences Open Settings (JSON)
and add a line "python.pythonPath": "/usr/bin/"
So JSON should look like
{
"python.pythonPath": "/usr/bin/"
}
Keep other configuration lines if it is present.
This should import all modules that you have installed using PIP for autocomplete.
回答15:
my solution was to open vscode in a previous directory.
回答16:
In my case I already had a conda environment activated but still wanted local python modules to be available for autocomplete, peeking definition, etc. I tried many solutions such as adding list of python paths etc.. but what finally solved it for me was to create a symlink from conda lib/python{your version}/site-packages
to my local module.