Search code examples
pythonwindowsvisual-c++cx-freeze

How to include a Microsoft Visual C++ Redistributable package in a python cx_freeze application


CONTEXT

I'm trying to build a python tkinter application using cx_freeze, and I successfully build the .exe. Here is my setup.py:

import sys
from cx_Freeze import setup, Executable

version = "0.7.6"
author = "Me"

base = None

if sys.platform == "win32":
    base = "Win32GUI"

build_exe_options = {"include_files": ["assets/", "files/"], "excludes": ["matplotlib.tests", "numpy.random._examples"], 
"include_msvcr": True}

setup(
    name="Application",
    version=version,
    description=f"Application {version}",
    author=author,
    options={"build_exe": build_exe_options},
    executables=[Executable(script="main.py", base=base, targetName="App.exe", icon="assets/Logo.ico")]
)

IDENTIFICATION OF THE PROBLEM

However, some libraries I use in the Python code, like matplotlib, can only be compiled with Visual Studio 2015 or later (see the documentation). I have Visual Studio 2019 on my computer so everything works fine on it but I tried running the app on another Windows computer which only has Microsoft Visual C++ 2010 Redistributable package (and I guess the 2010 compiling tools), and the app crashed before lauching with this main error:

ImportError: DLL load failed while importing ft2font: the specified module was not found.

Based on the discussions here, I figured this error means that Visual Studio requirements are not met (i.e 2015 or later) and as indicated at the end of this discussion, matplotlib wheels don't include C++ runtime DLLs for version > 3.3.0. Before asking my question, I must say my understanding of how python libraries are compiled or what wheels are is very limited and I don't even really know which C++ runtime DLLs are involved in the compilation of my built application.

QUESTION

How can I include Microsoft Visual C++ 2015 Redistributable package (assuming that is legal) in my app?

A simple solution for me would be to include all necessary dependancies in my project without having the user to install a Visual Studio C++ 2015+ version on its computer, so if you have any idea on how to achieve that with cx_freeze or even some other tools I might look into, I thank you in advance for your help.

Paul

P.S: as you can see, I tried the "include_msvcr" key from the build_exe_options but that didn't solve my problem (plus I think msvcp.dll is included by default)

UPDATE

I manually included vcruntime140_1.dll, msvcp140_1.dll in the directory and the app is now launching, but the window is not displayed correctly, its frame doesn't appear, so problem not solved yet.


Solution

  • If Windows Kits is installed, you should go to the C:\Program Files (x86)\Windows Kits location and decide on your appropriate python architecture. If the application is 64bit, a location type is created: C:\Program Files (x86)\Windows Kits\10\Redist\ucrt\DLLs\x64

    You must copy all the DLLs.

    However, this may not work well. I recommend running the dependency tool. This helps you find the right DLLs.

    Dependencies

    You should analyze the pythonVxVx.dll (python38.dll) file and the yourapp.exe file.