问题描述
我正在尝试使用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,CultureInfo culture)
at System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,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.figure"),"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一起使用会很棒,但这是一种解决方法,不需要太多的努力