I want to use request module, but whenever I tried to import requests I got this message:
import "requests" could not be resolved from source Pylance
I already used pip to install the requests module, but I still receive this error message.
I want to use request module, but whenever I tried to import requests I got this message:
import "requests" could not be resolved from source Pylance
I already used pip to install the requests module, but I still receive this error message.
Have you made sure that you have installed the module by running
pip install requests
or
pip3 install requests
Additionally, you can tell VS Code to use different Python-interpreter in your system. I e.g. had the same problem, that VS Code code check gave me the warning, that I don't have the module requests installed, but when I run the python script it worked anyhow. That's why I believe that the python code check in VS Code can use different Python versions and related python packages than your system use when you run the code.
What helped me in order to get rid of the VS Code error message, choosing a different Python from here:
I got the same error and solved it with this:
import pip._vendor.requests
If you are experiencing this problem on Windows, look at the installed extensions. I had this problem on both of my Windows machines. I noted that I had two extensions installed: Pylance & Intellisense (Pylance). Uninstalling Pylance and keeping intellisense (Pylance) solved the problem.
In my case the problem was that the Python interpreter being used was the default one instead on the virtual environment one. I had activated my venv and installed the requests package, but still was getting the error message. So I checked on the bottom right and noticed the default "global" interpreter was selected:
All I had to do was switch to the interpreter in my virtual environment (it even shows up as "Recommended"):
and it automatically started recognizing the package (no VSCode restart needed or anything).
I had this - and noticed that my workspace wasn't trusted.
This doesn't allow the pylance extension to check what modules are installed. Setting the workspace to trusted (bottom left on the status bar where it says restricted) allowed pylance to check for installed modules.
Failing this, other answers may apply.
Try choosing the python interpreter manually. Choose view - select interpreter - enter interpreter path - choose the python folder - app then python application. This should work
I had the same problem with importing requests
and BeautifulSoup
packages.
When I tried to install them by pip install requests
and pip install beautifulsoup4
I got the message that said "Requirement already satisfied: beautifulsoup4 in c:\programdata\anaconda3\lib\site-packages (4.9.3)".
So the way I solved this issue was by directly installing these packages to the version of Python I am currently using.
pip3.10.exe install requests
pip3.10.exe install beautifulsoup4
You can replace the part "3.10.exe" with your current version of Python.
Switching the interpreter in the VS code solved my problem. Went from 3.11.1 to 3.7 base - Conda.
If you are using VSCODE with a python virtual environment.
use ctrl+shift+p
or cmd+shift+p
to open the command palette.
Type python intepreter
in the command palette and select it.
The new environment can be found here, it will have a new prefixed with your project folder name.
If you don't find any similar names, try refreshing the python interpreters list
Just make sure that you explicitly set a file extension to .py
. But the answer by @Murat Büyükaksu works just fine with extension or w/out.
In my case I first had to upgraded pip by going to python 3 installable folder and running the command visual studio code was suggesting along with error when I first try to install requests package and then was able to run the following in command palette.
pip install requests
After installation, restart the code and after that code doesn't complain. my python version - 3.10.5
The solution is to change the interpreter. I solved this conflict by going to 3.8.8 (conda) from 3.9.9 (windows).
The problem is you might have installed the module in one env and not in the other one, so either install in each or try each of them!
Click on bottom-left: