Home > other >  (False) import error warning with global PyLint in virtual environment in Visual Studio Code
(False) import error warning with global PyLint in virtual environment in Visual Studio Code

Time:12-29

I use Visual Studio Code for coding in Python. In my python file, I stated the following imports:

import requests
from bs4 import BeautifulSoup

I created a virtual environment, installed all necessary packages with pip install inside the virtual environment and the python script runs fine.

In Visual Studio Code, I modified the settings to use PyLint which is installed on the global level (meaning the default python interpreter) so I don't need to install pylint in my virtual environment in order to avoid for it appearing in my requirements.txt. My .vscode/settings.json looks like this:

{
    "python.pythonPath": "/Users/pazifik/.virtualenvs/fun-image-scraper/bin/python",
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "/opt/homebrew/bin/pylint"
}

Visual Studio Code seems to pick up Pylint correctly, however in the "Problems" view it shows this:

Unable to import 'requests' pylint(importError) [3, 1]
Unable to import 'bs4' pylint(importError) [4, 1]

The reason is probably that the global installed PyLint cannot see the packages inside the virtual environment. The chosen Python interpreter in Visual Studio Code is the Python interpreter from the virtual environment.

How can I get rid of these (false) import errors in Visual Studio Code?

EDIT: To better visualize my problem and settings, here some screenshots:

pip list of installed packages in virtual environment Interpreter and list of packages in virtual env

pip show of requestspackage in virtual environment pip show of requests

"Problems" view (however! the script runs fine as mentioned, just the errors displayed there are confusing hence my question) Problems view

PyLint installed in global environment with default interpreter Pylint in default environment

CodePudding user response:

Very sorry for my previous answer, I had modified the pylintArgs before, so I have not found out the problem.

You can add this in the settings.json:

"python.envFile": "${workspaceFolder}/.env",

And create a file named .env under the workspace folder, and add this in it:

PYTHONPATH=/Users/pazifik/.virtualenvs/fun-image-scraper/lib/python3.9/site-packages

Explain:

Although you have selected the virtual environment in the VSCode, and you can find the PYTHONPATH has been modified in the terminal, but the pylint did not route through the terminal, so it will not get the modified PYTHONPATH.

When the terminal settings are used, PYTHONPATH affects any tools that are run within the terminal by a user, as well as any action the extension performs for a user that is routed through the terminal such as debugging. However, in this case when the extension is performing an action that isn't routed through the terminal, such as the use of a linter or formatter, then this setting will not have an effect on module look-up.

When PYTHONPATH is set using an .env file, it will affect anything the extension does on your behalf and actions performed by the debugger, but it will not affect tools run in the terminal.

You can refer to the enter image description here

The setting of python.pythonPath in the settings.json can not determine which python interpreter you are using.

And maybe you just installed the packages in the different environments, could you take the command of pip show {packagename} to check where the packages have been installed?

  • Related