Title: Python requests causes SSL Verification error on one Windows computer but not another
Tags: python-3.x,windows,python-requests,anaconda,ssl-certificate

Question: I'm building a windows service with Python 3.6 in an anaconda virtual environment. I make a post request using python requests: requests.post(url, files=files, data=data, headers=headers)
After creating the service, on my windows machine (the one that has the source code that created the service) this works right off the bat. When I install this service on another windows machine, I keep getting SSL: CERTIFICATE_VERIFY_FAILED. I installed it on a third windows machine and that works fine (but isn't the machine we need it to work on sadly).
Things I've tried:

Installed python-certifi-win32 with conda in my virtual environment before creating the service.
Specified a path to a .pem file with the chain of certificates for the url and added it with the verify parameter. So my request is as such: requests.post(url, files=files, data=data, headers=headers, verify='path\to\pemfile'). This works on my machine but not on the other one.

I printed out requests.certs.where() on both computers and they both say C:\Windows\TEMP\_MEXXXX\certifi\cacert.pem.
How can I get my service to run the same on all computers?
UPDATE: Reproducible example:
# debugFile.py
import servicemanager
import socket
import win32event
import win32service
import win32serviceutil
import traceback
import sys, getopt
import requests

class SCPWorker:
    def __init__(self):
        self.running = True

    def test_function(self):
        data = {}
        token = 'auth token for url'
        response = requests.post(custom_url, data=data, headers={'Authorization': "Token " + token})
                


class StoreScp(win32serviceutil.ServiceFramework):
    _svc_name_ = "Service"
    _svc_display_name_ = "Debug Service"
    _svc_description_ = "description"

 
    def __init__(self, args):
        self.worker = SCPWorker()
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        socket.setdefaulttimeout(60)

    def SvcStop(self):
        try:
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            win32event.SetEvent(self.hWaitStop)
            self.worker.stop()
            self.running = False
        except:
            servicemanager.LogErrorMsg(traceback.format_exc())

    def SvcDoRun(self):
        try:
            self.worker.test_function()
           
            while rc != win32event.WAIT_OBJECT_0 and rc != win32event.WAIT_FAILED and rc != win32event.WAIT_TIMEOUT and rc != win32event.WAIT_ABANDONED:
                rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)

            if rc == win32event.WAIT_OBJECT_0:
                servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                      servicemanager.PYS_SERVICE_STARTED,
                                      ('Service stopped', ''))
            else:
                servicemanager.LogMsg(servicemanager.EVENTLOG_ERROR_TYPE,
                                      servicemanager.PYS_SERVICE_STOPPED,
                                      ('Service quit unexpectedly with status %d' % rc, ''))
        except:
            servicemanager.LogErrorMsg(traceback.format_exc())


if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(StoreScp)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(StoreScp)

And then run pyinstaller -F --hidden-import=win32timezone DebugFile.py to create the exe. And then install the exe on a machine.

Answer: I never figured out why it didn't work on the other computer but I did manage to make a workaround work. First I ensured that my pyinstaller was at least 4.10 and then I installed pip-system-certs. Finally I added import pip_system_certs.wrapt_requests at the top of my python file. This library meant I had to install everything using pip and not conda.