44

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Betty Gandhi
  • 453
  • 1
  • 4
  • 7
  • 4
    Does this answer your question? [ModuleNotFoundError: No module named 'requests' using venv in vscode](https://stackoverflow.com/questions/58160360/modulenotfounderror-no-module-named-requests-using-venv-in-vscode) – Be Chiller Too Aug 18 '21 at 12:59
  • `use request module in vs code` that will not work because VSC is JavaScript (Node.js) based. VSC is the editor with which you create Python scripts that use `requests` – rioV8 Aug 18 '21 at 13:09
  • don't add quotes, read the python doc on `import` – rioV8 Aug 18 '21 at 13:09
  • @rioV8 It doesn't matter what Vscode is written in. Sounds like you've not used the Python extension. The quotes are part of the error in the pylance parser, not the actual code – OneCricketeer Aug 18 '21 at 13:13
  • in which python interpreter (virtual environment) have you installed it and which interpreter is used for running the script. Try to find the module in the File Explorer of your OS to be sure it is in the correct place – rioV8 Aug 18 '21 at 16:33
  • Did you use any virtual environment? Can you run `pip show requests` in integrated Terminal to check if its location is in `your current selected interpreter\lib\site-package`? – Molly Wang-MSFT Aug 19 '21 at 01:56
  • https://stackoverflow.com/questions/68832892/why-cant-i-import-requests-in-vs-code – AWAIS SADDIQUI Aug 30 '23 at 08:48

18 Answers18

62

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:

Screenshot of VS Code IDE. Change Python version for code check

Ofek Glick
  • 999
  • 2
  • 9
  • 20
Anthony
  • 904
  • 1
  • 8
  • 15
16

I got the same error and solved it with this:

import pip._vendor.requests 
  • 3
    Welcome and good suggestion! If you could explain why this works, or confirm that this is equivalent to the regular import, then that would help improve the experience for future readers. Thanks! – Salmonstrikes Sep 29 '21 at 11:31
  • This resolves my issue as well, but still we need to understand why we need to import like this. As in my previous VS code installation I can directly use normal import and it worked. – user0404 Jan 03 '22 at 05:34
  • Hey, thanks for this, as it works. But can you pease explain why? – Nithin Sai Jan 21 '23 at 14:36
  • For the ones wondering why this needed - it's because VSCode is not completely sure that it's a python. So, you have to explicitly clarify the library - just like you'd do for any reference in any language. But if you explicitly use a `.py` extension for this file, you wouldn't need that. – lentyai May 30 '23 at 22:17
8

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.

Chris Davies
  • 121
  • 1
  • 8
3

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:

Python version

All I had to do was switch to the interpreter in my virtual environment (it even shows up as "Recommended"): Recommended interpreter

and it automatically started recognizing the package (no VSCode restart needed or anything).

Floella
  • 1,279
  • 1
  • 22
  • 41
2

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.

Laice
  • 133
  • 6
1

Try choosing the python interpreter manually. Choose view - select interpreter - enter interpreter path - choose the python folder - app then python application. This should work

Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
  • This is not an answer. You should validate the answer that worked for you, and if needed give clarification in comments. – Floh Apr 22 '22 at 15:20
0

From VS code terminal set venv - C:\Python310\python.exe -m venv 'PATH_TO_VENV\.venv\'

After this verify packages are available

pip list

if required:

pip install requests

This solved my problem

eshirvana
  • 23,227
  • 3
  • 22
  • 38
GitJig
  • 9
  • 2
0

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.

0

I too got same error and it got resolved by giving url in ""(double quotes). enter image description here

enter image description here

S F
  • 11
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 24 '23 at 10:00
0

Switching the interpreter in the VS code solved my problem. Went from 3.11.1 to 3.7 base - Conda.

dmk
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33765268) – Drise Feb 08 '23 at 17:37
0

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

mingaleg
  • 2,017
  • 16
  • 28
0

It works after I switch the Python version. See the screenshot. enter image description here

Jian Zhang
  • 59
  • 1
  • 3
0

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.

lentyai
  • 534
  • 1
  • 9
  • 22
0

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

Saurabh
  • 81
  • 4
0

You command line interface has to be in the venv to install the "requests" API. enter image description here

Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
Henry
  • 1
0

Update version to 3.11.

1 - Click on version number in above

2 - Choice python version 3.11

3 - The problem solves here

-1

The solution is to change the interpreter. I solved this conflict by going to 3.8.8 (conda) from 3.9.9 (windows).

dmitry
  • 1
  • 2
-1
  1. Open interpreters
  2. Try all of them one by one

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:

enter image description here

Ethan
  • 876
  • 8
  • 18
  • 34
0xWick.eth
  • 11
  • 3