0

I have installed python 2.7 and also I have requests package installed. I am not sure of the reason, the site-packages are in a different location and my python 2.7 core files are in a different location

C:\Program Files\Python27
C:\Users\MyID\AppData\Roaming\Python\Python27\site-packages

I can see requests and the binaries in the path C:\Users\MyID\AppData\Roaming\Python\Python27\site-packages\requests. Yet when my job runs, it throws an error as shown below.

File "C:\Program Files (x86)\Jenkins\workspace\code\xxxx.py", line 36, in <module>
    import requests
ImportError: No module named requests

I have only one version of python installed.

C:\Users\MyID\Desktop\xxx\Automation\xxxxxx\venv\Scripts>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Murali Go
  • 29
  • 8
  • 1
    first, download python3.x .. python2.7 is no longer supported and people have been moving over for years. If you're learning python, python2.7 is not the way to go. You need to make sure python is installed and you install packages using the same user. You've pasted 4 different users in your paths above. If pip is somehow installing to the wrong user directories, I suggest trying the --user flag while using pip... or do what @Agent_Orange said – Derek Eden Jul 06 '20 at 03:25
  • Its a project requirement to use 2.7 and also i am the only user who is using it and i have edited my question accordingly. Sorry for confusing you up. Also i have tried uninstalling and installing python's request using the same command mentioned by @Agent_Orange but i get the same error. – Murali Go Jul 06 '20 at 03:28
  • lots of suggestions here: https://stackoverflow.com/questions/14295680/unable-to-import-a-module-that-is-definitely-installed sounds like it might be a path issue perhaps – Derek Eden Jul 06 '20 at 03:32
  • Even i guess it might be a path problem but what should i do? i have set the pythonpath environment variable as well. – Murali Go Jul 06 '20 at 03:45
  • you've added the site_packages folder to the pythonpath? – Derek Eden Jul 06 '20 at 03:47
  • have you checked `where python` in terminal to verify python installation? are you using a virtual environment? – Derek Eden Jul 06 '20 at 03:49
  • Yes I have added the site_packages folder to my pythonpath. Also when i pass where python command in terminal, it gives me the below output . C:\Users\MyID\Desktop\xxx\Automation\xxxxxx\venv\Scripts\python.exe C:\Program Files\Python27\python.exe – Murali Go Jul 06 '20 at 03:57
  • Yes. I am using virtual environment – Murali Go Jul 06 '20 at 03:57
  • I'm confused, where python seems to output two python.exe locations, perhaps it's using the wrong one? if you are using an venv then you must use that intepreter, and make sure the package is installed in the site_packages folder of that environment.. try running sys.executable from python shell and see which python you're actually using – Derek Eden Jul 06 '20 at 03:59

1 Answers1

0

This is most probably the problem of virtual environment, virtual environment (venv) isolate your work environment in such a way that you can have different workspace for same version of python, that's why you are getting the error, I suspect that you installed request module in original python, but trying to run the script in virtual environment, hence it's giving error

Here, try these steps and tell the result

  1. Open CMD
  2. Write cd C:\Users\MyID\Desktop\xxx\Automation\xxxxxx\venv\Scripts , Press enter.
  3. Write python -m pip install requests
  4. Wait for it and tell the result, this must fix your problem

For reference on virtual environment and pip, look at this.

Agent_Orange
  • 351
  • 1
  • 13
  • You have a virtual environment installed, having virtual environment your module will work only on the place where you install it, you have to install the requests module in virtual environment – Agent_Orange Jul 06 '20 at 04:07