Search code examples
pythonclrcx-freezepython.net

Issue with cx_freeze and pythonnet clr


I am attempting to build an application using pythonnet clr and cx_freeze, I have successfully built it with pyinstaller so I know it works but when using cx_freeze I and try to launch the .exe is receive the following error:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the 
target of an invocation. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: key
   at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at Python.Runtime.Runtime.InitializePlatformData()
   at Python.Runtime.Runtime.Initialize(Boolean initSigs)
   at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv, Boolean initSigs)
   at Python.Runtime.PythonEngine.InitExt()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at clrModule.PyInit_clr()

my setup.py is as follows:

from cx_Freeze import setup, Executable
import sys
import matplotlib
import site
from mypackage import version

mpl_toolkits = site.getsitepackages()[1] + '/mpl_toolkits'


def getTargetName():
    """OS specific name for application"""
    app_name = "myapp"
    if sys.platform.startswith('linux'):
        return app_name
    elif sys.platform.startswith('win'):
        return app_name + ".exe"
    else:
        return app_name + ".dmg"


# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os", 'sys', 'pyqt5', 'serial.win32', 'numpy', 'pubsub', 'mypackage', 'clr'],
                     "includes": [],
                     "include_files": [(matplotlib.get_data_path(), "mpl-data"),
                                       (matplotlib.get_data_path(), 
"matplotlib.backends.backend_qt5agg"),
                                       (matplotlib.get_data_path(), "matplotlib.figure"),
                                       (matplotlib.get_data_path(), "matplotlib.dates"),
                                       (mpl_toolkits, "mpl_toolkits")],
                     "excludes": ["tkinter", "matplotlib.tests", "numpy.random._examples"]}

# GUI applications require a different base on Windows (the default is for a console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name="myapp",
      version=version.VERSION,
      description="application",
      options={"build_exe": build_exe_options},
      executables=[Executable("mypackage/main.py",
                              icon="mypackage/logo.ico",
                              shortcutName="my app",
                              shortcutDir="DesktopFolder",
                              targetName=getTargetName(),
                              base=base)])

I have successfully built the application before adding pythonnet clr components, any insight would be appreciated


Solution

  • As I was having diffculty getting pythonnet to work with cx_freeze, I seperated my application into two applications. I used PyInstaller to package the pythonnet application into an executable which was called from the main application. I then used cx_freeze to package the main application into an executable and packaged both applications into an msi package which I was after.

    Would be great to get cx_freeze to work with pythonnet but this is a work around that doesnt involve too much effort