0

I have requests module installed but it shows an error when running .py file from cmd prompt. There are no errors when running the file from vscode. enter image description here

Jaadu
  • 21
  • 2
  • Did you install **requests** via command prompt? – Ghantey Apr 29 '22 at 11:04
  • Do the answers to this [question](https://stackoverflow.com/questions/17309288/importerror-no-module-named-requests) help at all? – quamrana Apr 29 '22 at 11:10
  • Does this answer your question? [ImportError: No module named requests](https://stackoverflow.com/questions/17309288/importerror-no-module-named-requests) – Lenormju Apr 29 '22 at 11:11

3 Answers3

0

It seems that requests is installed in your virtual enviroment "my_env" but you must execute your script using the intepreter of your virtual enviroment (It seems that windows keep using the base interpreter even if you activated the virtual env). Try calling it directly, for example

"Your_path_to_venv\Scripts\python.exe" main.py
Gam
  • 318
  • 2
  • 9
0

This issue can be caused by multiple issues.

  1. Wrong pip version is in path (Caused by installing a newer version of python, while an older version was already there.

This can be fixed by simply removing the old python from PATH and restarting the command line. enter image description here

enter image description here

enter image description here

enter image description here

  1. Try using pip3 instead of pip maybe pip installs libraries in a different dictionary than pip3 does

If that didn't work then use

python -m pip install “your library” And if you were using python3 just do the same

python3 -m pip install ”your library” If they did not work too, replace pip in the last two commands with pip3

And if it still does not work first see the path of the python site-packages files by running this python code

import os 
import inspect 
print(os.path.dirname(inspect.getfile(inspect))+"/site-packages") 

a path will be printed, take it and add it as a parameter to your pip command

pip install -t “the printed path”

Aly Mobarak
  • 356
  • 2
  • 17
0

Ok, I see that you probably have a script called requests.py in your C:\Users\BokaBu>pyproj\my_env\Scripts\activate folder, but your question is how can C:\Users\BokaBu>pyproj\src\main.py find it.

The solution is to add C:\Users\BokaBu>pyproj\my_env\Scripts\activate to PYTHONPATH. The solution to the general version of your question can be found here:

Importing files from different folder - Stackoverflow

But in more detail, you may want to try this to your C:\Users\BokaBu>pyproj\src\main.py:

# In C:\Users\BokaBu>pyproj\src\main.py

import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, 'C:\Users\BokaBu>pyproj\my_env\Scripts\activate')

import requests

Hope it helps~

Mark
  • 336
  • 1
  • 7