Brandon Meyer
Brandon Meyer

Reputation: 108

ModuleNotFoundError: No module named 'requests' using venv in vscode

I have 1 file, i get as far as line 1 import requests, nothing more yet and I receive the following error ModuleNotFoundError: No module named 'requests'.

  1. Using Visual Studio Code, installed today with standalone Python x64 3.7.4
  2. Python Extension installed in VSCODE
  3. venv created within VSCODE as a subfolder of workspace withint VSCODE terminal and was recognized by VSCODE when created and I am using venv interpreter in VSCODE as indicated on bottom bar
  4. ran pip install requests within terminal inside VSCODE - completed successfully
  5. ran pip freeze to confirm requests==2.22.0 is registered
  6. Verified pylint was installed in venv

I cannot figure out why VSCODE python interpreter will not recognize that requests is installed

Upvotes: 6

Views: 45896

Answers (6)

Fen
Fen

Reputation: 81

2 ways work for me, under windows 10:

  1. Run in terminal: CTRL + F5
  2. Add the following in settings json:
"code-runner.executorMap": {
  "python": "$pythonPath -u $fullFileName"
}

Upvotes: 1

radhadman
radhadman

Reputation: 115

Try using:

pip3 install requests

Upvotes: 0

Nico_Robin
Nico_Robin

Reputation: 89

Have you checked your user and workspace settings?

Make sure your workspace settings (not your user settings) points to your project's venv path.

I have encountered a similar problem such as yours but was able to solve it by changing the workspace settings python path to point to my venv path as below.

{
    "python.pythonPath": "${workspaceFolder}/code/venv/bin/python"
}

Upvotes: 5

Will Wu
Will Wu

Reputation: 631

try get python version in vscode terminal

python --version

and check python version vscode IDE used by clicking left buttom corner. enter image description here

make sure these 2 versions are consistent. if not, select the version of IDE with the same version of your terminal.

Upvotes: 14

Brandon Meyer
Brandon Meyer

Reputation: 108

Thanks for the responses. I found out my own issue after a few hours.

First off I figured out somewhere that when I was running the terminal in VSCODE it was running against global interpreter. I then tried ./pip freeze and it worked as expected. This told me that the terminal was connected to the wrong interpreter.

For some reason when I would 'run' the file it was executing in the terminal but it wasn't 'activating' the venv. I've closed and reopened VSCODE many times but for some reason when I closed the terminal window and reopened using CTRL+SHIFT+' and I received an error about signed scripts when it tried to activate my venv using the PS1 script. I ran Set-ExecutionPolicy -ExecutionPolicy RemoteSigned inside a normal PowerShell window and then reopened the Terminal window in VSCODE and it successfully activated my venv and I know see my venv name at the beginning of the line of the terminal. I then ran 'pip freeze' to confirm which modules installed inside of venv and then ran again in normal powershell window and it was different.

There is documentation somewhere that says to run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned which I found later, but it would have been nice to know this was a requirement.

Upvotes: 2

oaixnah
oaixnah

Reputation: 44

Is there more than one python environment locally? maybe try:

pip3.7 install requests

Upvotes: 1

Related Questions