python requests and cx_freeze

2024/10/17 15:25:27

I am trying to freeze a python app that depends on requests, but I am getting the following error:

Traceback (most recent call last):File "c:\Python33\lib\site-packages\requests\packages\urllib3\util.py", line 630, in ssl_wrap_socketcontext.load_verify_locations(ca_certs)
FileNotFoundError: [Errno 2] No such file or directory

Looks like it is having trouble finding the ssl certificate with the executable. I found this which seems to be the same problem, but I am not able to figure out how they got it to work. The main problem seems to be that the certificate bundled by requests is not copied over to the compressed library. So it seems that I will have to force cx_freeze to bundle the certificates and then point to it from my script.

Starting with this simple script everything works fine:

import requests
r = requests.get("https://yourapihere.com")
print(r.json())

Then if I add the certificate file I stat getting errors:

import requests
r = requests.get("https://yourapihere.com", cert=requests.certs.where())
print(r.json())

-

Traceback (most recent call last):File "c:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 480, in urlopenbody=body, headers=headers)File "c:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 285, in _make_requestconn.request(method, url, **httplib_request_kw)File "c:\Python33\lib\http\client.py", line 1065, in requestself._send_request(method, url, body, headers)File "c:\Python33\lib\http\client.py", line 1103, in _send_requestself.endheaders(body)File "c:\Python33\lib\http\client.py", line 1061, in endheadersself._send_output(message_body)File "c:\Python33\lib\http\client.py", line 906, in _send_outputself.send(msg)File "c:\Python33\lib\http\client.py", line 844, in sendself.connect()File "c:\Python33\lib\site-packages\requests\packages\urllib3\connection.py", line 164, in connectssl_version=resolved_ssl_version)File "c:\Python33\lib\site-packages\requests\packages\urllib3\util.py", line 637, in ssl_wrap_socketcontext.load_cert_chain(certfile, keyfile)
ssl.SSLError: [SSL] PEM lib (_ssl.c:2155)During handling of the above exception, another exception occurred:Traceback (most recent call last):File "c:\Python33\lib\site-packages\requests\adapters.py", line 330, in sendtimeout=timeoutFile "c:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 504, in urlopenraise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2155)During handling of the above exception, another exception occurred:Traceback (most recent call last):File "example.py", line 10, in <module>r = requests.get("https://yourapihere.com", cert=requests.certs.where())File "c:\Python33\lib\site-packages\requests\api.py", line 55, in getreturn request('get', url, **kwargs)File "c:\Python33\lib\site-packages\requests\api.py", line 44, in requestreturn session.request(method=method, url=url, **kwargs)File "c:\Python33\lib\site-packages\requests\sessions.py", line 383, in requestresp = self.send(prep, **send_kwargs)File "c:\Python33\lib\site-packages\requests\sessions.py", line 486, in sendr = adapter.send(request, **kwargs)File "c:\Python33\lib\site-packages\requests\adapters.py", line 385, in sendraise SSLError(e)
requests.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2155)

I guess I am using it correctly, but cant really figure out why it is not working. I guess that after fixing this I can continue and add the certificate to the cx_freeze bundle, something like:

example.py:

import os
import requestscert = os.path.join(os.path.dirname(requests.__file__),'cacert.pem')
r = requests.get("https://yourapihere.com", cert=cert)
print(r.json())

setup.py:

from cx_Freeze import setup, Executableimport requests.certs
build_exe_options = {"zip_includes":[(requests.certs.where(),'requests/cacert.pem')]}executables = [Executable('example.py')
]setup(executables=executables)

if someone could give me a tip it would be much appreciated.

Answer

I found this comment from another thread that worked for me: https://stackoverflow.com/a/25239701/3935084

Summary below:

You can also use environment variable "REQUESTS_CA_BUNDLE" (as said http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification)

It's much simpler than correcting all your requests:

os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.getcwd(), "cacert.pem")
https://en.xdnf.cn/q/72998.html

Related Q&A

django changing a date field to integer field cant migrate

I recently changed a date field to an integer field (the data was specified in number of months remaining rather than a date). However all though the make migrations command works fine when I attempt t…

Sqlalchemy get row in timeslot

I have a model called Appointment which has the columns datetime which is a DateTime field and duration which is an Integer field and represents duration in minutes. Now I want to check if func.now() i…

How do I include non-.py files in PyPI?

I am a newb to PyPI...so let me qualify with that. I am trying to put a package on PyPI but having a bit of trouble when I try to install it with pip. When I upload the file to PyPI, I get a warning (b…

How to create a custom AutoField primary_key entry within Django

I am trying to create a custom primary_key within my helpdesk/models.py that I will use to track our help desk tickets. I am in the process of writing a small ticking system for our office.Maybe there …

Multiple HoverTools for different lines (bokeh)

I have more than one line on a bokeh plot, and I want the HoverTool to show the value for each line, but using the method from a previous stackoverflow answer isnt working:https://stackoverflow.com/a/2…

Cant Install PIL 1.7

I have python 2.7.3 and I want to install PIL 1.7. I downloaded "PIL-1.1.7.win32-py2.7" and try to install it but it shows me an error messege that it cant find python 2.7 in the registry.&qu…

slice/split a layer in keras as in caffe

I have used this converter to convert a Caffe model to Keras. But one of my layers is of type slice and it needs to be converted as well but the converter currently does not support this and raises an …

Can I use regexes within datetime.strptime formats?

I have string values that contain a trailing time stamp. I thought I could use strptime with a regex pattern to extract those. Like:from __future__ import print_functionfrom datetime import datetime # …

Sort a list with longest items first

I am using a lambda to modify the behaviour of sort.sorted(list, key=lambda item:(item.lower(),len(item)))Sorting a list containing the elements A1,A2,A3,A,B1,B2,B3,B, the result is A,A1,A2,A3,B,B1,B2,…

Multivariate Root Finding in Python

Using excel solver, it is easy to find a solution (optimum value for x and y )for this equation: (x*14.80461) + (y * -4.9233) + (10*0.4803) ≈ 0However, I cant figure out how to do this in Python. The …