简体   繁体   中英

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

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"
}

try get python version in vscode terminal

python --version

and check python version vscode IDE used by clicking left buttom corner. 在此处输入图像描述

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

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.

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

pip3.7 install requests

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"

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM