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
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.