4

I was trying out Python for the first time today and I ran into a problem. I want to import requests, but it shows an error in the code:

Error: Import "requests" could not be resolved from source Pylance(reportMissingModelSource) [4, 8]

I am using Visual Studio Code.

I've tried pip install requests and it looks good, but I'm still getting an error in my code.

When I do pip install requests:

(Removed locations except for first)

PS C:\Users\me\Desktop\python testing01> pip install requests
Requirement already satisfied: requests in c:\users\me\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (2.26.0)
Requirement already satisfied: idna<4,>=2.5 in
Requirement already satisfied: charset-normalizer~=2.0.0 in
Requirement already satisfied: urllib3<1.27,>=1.21.1 in
Requirement already satisfied: certifi>=2017.4.17 in
WARNING: You are using pip version 21.1.3; however, version 21.2.4 is available.
You should consider upgrading via the 'C:\Users\me\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip' command.

Relevant Python code:

import random

import requests
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wesley Smith
  • 59
  • 1
  • 3
  • 1
    What error do you get? – Olvin Roght Aug 29 '21 at 06:54
  • Error: Import "requests" could not be resolved from source Pylance(reportMissingModelSource) [4, 8] @OlvinRoght – Wesley Smith Aug 29 '21 at 07:00
  • Related: [Import “flask” could not be resolved from source Pylance (reportMissingModuleSource)](https://stackoverflow.com/q/65786221/10824407) – Olvin Roght Aug 29 '21 at 07:05
  • 1
    Okay, I am assuming that you're using VS Code. (If so, please edit your original question to include this information.) Are you sure that you have selected the global interpreter rather than some other virtual environment from a previous session? See this for how to check that: https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment – navneethc Aug 29 '21 at 07:05
  • Also, check if it's a duplicate of https://stackoverflow.com/questions/65786221/import-flask-could-not-be-resolved-from-source-pylance-reportmissingmodulesou – navneethc Aug 29 '21 at 07:09

4 Answers4

1

First of all, uninstall request by the following command:

pip uninstall requests

Then you have to install it again by the following command:

pip install requests

Now you have to import requests in the following way:

import requests
0

Upgrade pip. Try:

pip install --upgrade pip

If your OS is Windows and if this command doesn't work, try:

python -m pip install --upgrade pip

Then try to install requests. It should work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

For me, I had to :

  1. Upgrade pip
    pip install --upgrade pip
  1. Uninstall requests
    pip uninstall requests
  1. Install requests
    pip install requests
  1. Then reboot VsCode
0

I had to install in the terminal and then it worked. Would not work when attempting in my IDE, (VS Code).

  • Sometimes your IDE runs a different Python environment then the one that your terminal defaults to, you may want to check what Python environment it's running. – Brock Brown Apr 19 '23 at 11:35