2

I am having a problem running my script in Visual Studio Code.

I have tried the following:

  1. Reinstalling requests through pip, easy_install, and sudo pip
  2. Importing requests directly in the Python interpreter (which worked)

This leads me to believe that Visual Studio Code isn't using the correct interpreter that actually has the package installed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Frank Fiumara
  • 120
  • 1
  • 15
  • Are you using a virtual environment? I remember having this issue with `requests` (not on MacOS nor with VSCode, but on Windows with plain-old IDLE); while I don't know what causes this behavior, I was able to solve it by creating a virtual environment and configuring my IDE (switched to PyCharm) to use that virtual environment interpreter. – Jacob Lee May 11 '21 at 22:29
  • I am not using a vm. Running on a 2019 MacBook Pro with the latest version of Big Sur. Thanks for the advice in case I use a VM in the furture – Frank Fiumara May 12 '21 at 04:39

3 Answers3

2

You can choose your interpreter in VS Code in several ways. One of them is by clicking the tab in the bottom toolbar (lower-left) which might say "Python 3.9.4 (64-bit)", upon which you will be able to select from a menu of choices, e.g. /usr/bin/python3 or a conda or brew installation, etc.

Jacob Walls
  • 873
  • 3
  • 15
1

I think yours is a Python environment problem. To check whether it's Python environment or not, you can use which python (or which python3 if you use Python 3) command in your both Visual Studio Code terminal and Mac terminal. If you see different python path, then your Visual Studio Code is using different python environment. You can change the Python interpreter in Visual Studio Code to have same environment as your Mac terminal.

  • Open Command Palette in Visual Studio Code with + + P
  • Type "Python: Select Interpreter"

Then, choose the same environment as your Mac terminal (if you want the same one, you can choose the same Python environment path as Mac terminal that you get with "which python" or "which python3").

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chuu
  • 176
  • 7
0

You may be able to find a solution here: ImportError: No module named requests

You haven't mentioned using different versions in your commands. It's possible you need to use pip3 instead of pip, or python3 instead of python.

Robin Duong
  • 61
  • 1
  • 10