python requests cant find a folder with a certificate when converted to .exe

python requests cant find a folder with a certificate when converted to .exe

I ran into this problem as well. It looks like it comes from the certificate bundle cacert.pem not being included in the requests package directory when the program is compiled. The requests module uses the function certifi.core.where to determine the location of cacert.pem. Overriding this function and overriding the variables set by this function seems to fix the problem.

I added this code to the beginning of my program:

import sys, os


def override_where():
     overrides certifi.core.where to return actual location of cacert.pem
    # change this to match the location of cacert.pem
    return os.path.abspath(cacert.pem)


# is the program compiled?
if hasattr(sys, frozen):
    import certifi.core

    os.environ[REQUESTS_CA_BUNDLE] = override_where()
    certifi.core.where = override_where

    # delay importing until after where() has been replaced
    import requests.utils
    import requests.adapters
    # replace these variables in case these modules were
    # imported before we replaced certifi.core.where
    requests.utils.DEFAULT_CA_BUNDLE_PATH = override_where()
    requests.adapters.DEFAULT_CA_BUNDLE_PATH = override_where()

This might be an issue with requests package.

I solved this by manually copying the cacert.pem file from /lib/site-packages/certifi to /lib/site-packages/requests

If you want to fix this issue with .exe, then copy cacert.pem file from /lib/site-packages/certifi to dist/library.zip/certifi/.

I am considering you have created exe using py2exe, where py2exe will create library.zip under dist/ which contains of all script dependencies. I dont know if other exe converters create library.zip.

python requests cant find a folder with a certificate when converted to .exe

If you want to disable certificate verification, you can use the verify=False parameter in requests.get():

requests.get(https://example.com, verify=False)

Leave a Reply

Your email address will not be published. Required fields are marked *