python requests can't find a folder with a certificate when converted to .exe

前端 未结 2 1418
生来不讨喜
生来不讨喜 2021-01-04 18:21

I have a program that pools ad stats from different marketing systems. Everything works fine untill i convert it to the .exe format and run it.

Exception in          


        
2条回答
  •  梦毁少年i
    2021-01-04 18:49

    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()
    

提交回复
热议问题