'VS Code doesn't recognize Python Virtual Environment Packages - It shows red underlines
I installed python virtual env. I use vs code. I imported scrapy in my code and vs code doesn't recognize the module. Actually, it works well when I run it. scrapy crawl tester
Just, vs code shows red underlines that mean "Unable to import scrapy" So this is just vs code issue, not venv or scrapy package install issue.

This code works well and actuallay, scrapy is imported without any issue. This is just vs code issue. Thank you.
Solution 1:[1]
You might be able to solve your problem by using CTRL+Shift+P to add "Python: Select Interpreter" to your project.
Solution 2:[2]
If you created a virtual environment and activated it as well
type which python on macOS/Linux,
type where python on Windows,
inside the activated terminal session.
(env) userpc@pc:~$ which python
/home/userpc/Desktop/foldername/env/bin/python
In VSCode press Ctrl+Shift+P, under >Select Interpreter paste the location you receive using the former command.
Once done, restart VSCode. VSCode will also ask you to install pylint in your environment and if it doesn't you can do the same by activating you environment and typing
pip install pylint
inside the activated terminal session.
Solution 3:[3]
Ran into the same problem - selected the correct Python interpreter in VS Code, pip installed all the desired Python libraries but import is still underlined in VS Code.
What you need to do
What worked for me is to make sure that the linter that you are using in this VS Code instance (I was using pylint) is from the bin folder of the virtual environment, not somewhere else.
How you can do it
I'll use the absolute path to the desired virtual environment /User/ProjectFolder/env as an example.
To check that you meet the conditions stated in What you need to do, toggle the settings.json file in VS Code by pressing ? + ,:
- Make sure that the correct Python interpreter is selected. i.e.
"python.pythonPath":/User/ProjectFolder/env/bin/python3. - Make sure that the linter (e.g. pylint) is located in that
binfolder, not anywhere else. i.e"python.linting.pylintPath":/User/ProjectFolder/env/bin/pylint, NOT something like"python.linting.pylintPath":/usr/local/bin/pylint.
This means that you have to install your desired linter in the virtual environment.
Hope this helps.
Solution 4:[4]
I tried @cleon-w's answer which worked for me. (Thanks Cleon)
I was using pyenv on my Mac M1 (Big Sur) with vscode. vscode could not resolve the paths to python that the pyenv provides, so I looked within the pyenv scripts to find the absolute paths to the underlying python and the pylint files.
As @Cleon W says I ensured pylint and python are in the same bin directory.
Then edit VS Code settings.json to point to them directly (bypass .pyenv) and the imports could be found.
settings.json (vscode)
"python.defaultInterpreterPath": "/Users/USERNAME/.pyenv/shims/python",
"python.pythonPath": "/Users/USERNAME/.pyenv/versions/3.9.1/bin/python",
Solution 5:[5]
I have similar problem with Django. What solved my problem was:
- create .vscode/settings.json file inside root dir for project.
- add this json {"python.defaultInterpreterPath": "path to bin directory in root dir for project"}
- Ctrl-Shif-P -> Python: Select Interpreter -> select one that said: Use Python from
python.defaultInterpreterPath. If it's not in the list, hit refresh (circled refresh button on the top of settings box).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nicolas Bisson |
| Solution 2 | rohanvtk |
| Solution 3 | |
| Solution 4 | Gregory Belton |
| Solution 5 | ferrislav |
