Fernando Freitas Alves Fernando Freitas Alves - 3 months ago 19
Python Question

Error using Requests in a frozen app

I am trying to use the excelent requests library in a frozen app. The code works fine when interpreted, but it stops working when I generate the dist executable.

I tried this solution, but it is not working (Requests library: missing file after cx_freeze)

My setup.py file:

import esky.bdist_esky
from esky.bdist_esky import Executable as Executable_Esky
from cx_Freeze import setup, Executable
from myapp import VERSION
import requests.certs

packages = [
'PIL',
'_winreg',
'esky',
]

includes = [
'PySide',
'sys',
'os',
'datetime',
'threading',
'Queue',
'uuid',
'requests',
]

excludes = [
'TKinter',
'tcl',
'ttk',
]

include_files =["icon-16px.ico",
"icon-32px.ico",
"logo-t-160x56.png",
]

setup(
scripts = [
Executable_Esky(
"myapp.py",
gui_only = False,
icon = "icon-16px.ico",
),
],
data_files = include_files,
options={"build_exe":
{"packages":packages,
"includes": includes,
"include_files": include_files + [(requests.certs.where(),'cacert.pem')],
"excludes": excludes,
"optimize": 2,
"icon":"icon-16px.ico",
},
"bdist_esky":{
'freezer_module':"cxfreeze",
'includes': includes,
'excludes': excludes,
},
},
executables = [Executable(script="myapp.py",base="Win32GUI")],
)


Traceback:

Traceback (most recent call last):
File "C:\Users\Fernando\Dropbox\the all-seeing boss\myapp_client\testes\cx
_freeze\qt_gui\interface_qt.py", line 45, in login
r = requests.post(url, data=data)
File "C:\Python27\lib\site-packages\requests\api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 383, in reques
t
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 385, in send
raise SSLError(e)
requests.exceptions.SSLError: [Errno 185090050] _ssl.c:343: error:0B084002:x509
certificate routines:X509_load_cert_crl_file:system lib

Answer

I got this following this issue on github(https://github.com/kennethreitz/requests/issues/557#issuecomment-6420819). Thank you Martjin Pieters for this tip.

First of all, I put the cacert.pem file in my project's folder and then I included this in the frozen app:

include_files =["icon-16px.ico",
                "icon-32px.ico",
                "logo-t-160x56.png",
                "cacert.pem",
                ]

setup(
    scripts = [
        Executable_Esky(
            "myapp.py",
            gui_only = False,
            icon = "icon-16px.ico",
        ),
    ],
    data_files = include_files,
    options={"build_exe":
                 {"packages":packages,
                  "includes": includes,
                  "include_files": include_files,
                  "excludes": excludes,
                  "optimize": 2,
                  "icon":"icon-16px.ico",
                 },
             "bdist_esky":{
                 'freezer_module':"cxfreeze",
                 'includes': includes,
                 'excludes': excludes,
             },
    },
    executables = [Executable(script="myapp.py",base="Win32GUI")],
)

And for the last you must indicate this certificate in each POST or GET using requests, like this:

r = requests.post(url, data=data, verify = os.path.join(appdata,'cacert.pem'))

XD