I ran into some problems with using cx_freeze to create executables from python code. The built executables run from the Anaconda prompt, but when run in windows cmd (or via click on the .exe) the program exits with an error message related to the requests package.
Here is a minimal working example. First, the script that will be frozen: main.py
import requests
r = requests.get('https://github.com/')
print(r.text)
Secondly, the setup script for cx_freeze: setup.py
import cx_Freeze
build_exe_options = {
"includes": [],
'include_files': [],
'excludes' : [],
'packages' : []
}
base = None
executables = [cx_Freeze.Executable('main.py',
base=base)]
cx_Freeze.setup(name = "MWE",
options = {"build_exe": build_exe_options},
executables = executables)
Here is the problem:
- Running
main.pyin python works as expected - Building the executable via
python setup.py buildworks fine - Running the resulting
main.exein the Anaconda prompt works as expected Running
main.exein the windows cmd (or starting via mouse-click) exits with the following error message:Traceback (most recent call last): File "P:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 40, in run module.run() File "P:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 23, in run exec(code, {'__name__': '__main__'}) File "main.py", line 9, in <module> r = requests.get('https://github.com/') File "P:\Anaconda3\lib\site-packages\requests\api.py", line 75, in get return request('get', url, params=params, **kwargs) File "P:\Anaconda3\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "P:\Anaconda3\lib\site-packages\requests\sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "P:\Anaconda3\lib\site-packages\requests\sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "P:\Anaconda3\lib\site-packages\requests\adapters.py", line 514, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: / (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))
After digging through related issues it seems the problem is that some DLLs are not copied into the build-folder of the application, such that these files cannot be found by the application. What I tried without positive result:
- Played with the
includesandpackagesoptions in the setup script to include certain packages (as describe here) - Manually copied
libsslandlibcryptoto the build-folder (as described here and here) - Copied the same files via the
include_filesoption in the setup script - Added
(requests.certs.where(),'cacert.pem')toinclude_filesandos.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.getcwd(), "cacert.pem")tosetup.pyas described here and here
Does anyone know how to avoid this issue? Does anyone know how to find out which files exactly are missing, such that they can be copied to the application folder? I ran into the same problem with other packages, not only requests.