cx_freeze 和 pythonnet clr 问题

Issue with cx_freeze and pythonnet clr

我正在尝试使用 pythonnet clr 和 cx_freeze 构建一个应用程序,我已经使用 pyinstaller 成功构建了它,所以我知道它可以工作但是当使用 cx_freeze 我并尝试启动 .exe收到以下错误:

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()

我的setup.py如下:

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)])

在添加 pythonnet clr 组件之前我已经成功构建了应用程序,任何见解将不胜感激

因为我很难让 pythonnet 与 cx_freeze 一起工作,所以我将我的应用程序分成了两个应用程序。我使用 PyInstaller 将 pythonnet 应用程序打包成一个从主应用程序调用的可执行文件。然后我使用 cx_freeze 将主应用程序打包成一个可执行文件,并将这两个应用程序打包成一个我想要的 msi 包。

让 cx_freeze 与 pythonnet 一起工作会很棒,但这是一个不需要太多努力的解决方法