Troubleshooting "python requests not recognized by pylance"

Feb 3, 2024 ยท 2 min read

When working with Python in Visual Studio Code, you may come across an error like "requests is not accessed" reported by the Pylance language server. This can be frustrating, since requests is a very common Python library. Here are some things to check to resolve this issue.

The root cause is that Pylance does not have full knowledge of the requests library when it analyzes your code. Pylance relies on stub files to determine what modules, functions, and variables are available. If it's missing stubs for requests, it will think import requests or requests.get are invalid.

Ensure Pylance is installed

First, make sure Pylance is actually installed in VS Code. Open the extensions sidebar, search for "pylance," and verify it is enabled. If not, install the extension from the marketplace. Pylance comes bundled with Python support in recent versions of VS Code.

Check your Python interpreter

Next, check that your selected Python interpreter is supported by Pylance. Click on the Python version in the bottom bar and ensure it points to a CPython 3.6+ install that Pylance can analyze. If you use virtual environments, activate the venv first.

Allow remote stub downloads

Finally, make sure Pylance has permissions to download stub files from its remote repository. Go to File > Preferences > Settings and search for "pylance stub." Ensure the "Stub Download" setting does not block downloads over the internet.

After checking these points, close and reopen your Python file to restart Pylance, or simply try running/building your program. This should allow Pylance to grab the latest stubs containing requests and resolve that error.

The key is making sure Pylance has access to stub files over the network so it can properly analyze third-party libraries like requests.

Browse by tags:

Browse by language:

Tired of getting blocked while scraping the web?

ProxiesAPI handles headless browsers and rotates proxies for you.
Get access to 1,000 free API credits, no credit card required!