2

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:

  1. Running main.py in python works as expected
  2. Building the executable via python setup.py build works fine
  3. Running the resulting main.exe in the Anaconda prompt works as expected
  4. Running main.exe in 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:

  1. Played with the includes and packages options in the setup script to include certain packages (as describe here)
  2. Manually copied libssl and libcrypto to the build-folder (as described here and here)
  3. Copied the same files via the include_files option in the setup script
  4. Added (requests.certs.where(),'cacert.pem') to include_files and os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.getcwd(), "cacert.pem") to setup.py as 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.

Fgop
  • 151
  • 1
  • 8
  • I don't know a lot about `cx_freeze` but I was interested in the problem: when googling I found that there seem to be a few issues like yours with cx_freeze version 5.x. What version are you using? Your example works perfectly fine for me (starting it by double-clicking the exe and from Windows CMD both work), no workarounds required at all (using `cx_freeze` version 6.0). – orangeInk Nov 14 '19 at 08:14
  • Interesting. That defeats the purpose of my MWE... I'm also on `cx_freeze` 6.0 and set up a whole new conda env in python 3.7 to test this. So all packages are up to date. Did my example run for you without any adjustments? Are you using Anaconda or a different python environment? – Fgop Nov 14 '19 at 09:37
  • I used a fresh conda env with Python 3.7 I did have to make one adjustment but didn't mention it since it seemed unrelated: when I ran your MWE I got `ImportError: No module named 'queue'` which I resolved by changing the `build_exe_options` in your `setup.py` to include `'packages' : ["multiprocessing"]` (taken from this post: https://stackoverflow.com/questions/40768570/importerror-no-module-named-queue-while-running-my-app-freezed-with-cx-freeze) – orangeInk Nov 14 '19 at 11:30
  • Here's a [pastebin](https://pastebin.com/zi5qDm9V) of my dependencies for your MWE in case you're interested. – orangeInk Nov 14 '19 at 11:39
  • Thanks, I didn't get that error. I will still try whether your solution works for me. Will report back. – Fgop Nov 15 '19 at 09:22

0 Answers0