"import clr" 使用 cx_freeze 进行精细编译后出现问题

"import clr" issue after fine compilation with cx_freeze

我的 import clr 出现问题,导致我的小程序 .exe 崩溃。

以下是我在程序中使用的导入:

import sys
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from PyQt4.QtGui import QApplication, QMainWindow
from PyQt4.Qt import QGridLayout, QWidget
import clr
executablePath = 'E:\PythonWS\MyDll\'
sys.path.append(executablePath)
clr.AddReference("MyDll")
import MyDll

基本上程序编译正常并且我得到了一个可执行文件感谢 cx_freeze,但是当我用日志启动它时,它在 import clr

时崩溃

我不知道该怎么做才能让它发挥作用,如果有人有想法?

编辑: 我在 import clr 周围添加了一个 try-except,如下所示:

try:
    logger.info('in try')
    import clr
    executablePath = 'E:\PythonWS\MyDll\'
    sys.path.append(executablePath)
    clr.AddReference("MyDll")
    dllPath = clr.FindAssembly('MyDll')
    import MyDll
    ts = MyDll.TestSystem('127.0.0.1', '127.0.0.1')
    print ts.mainBoard.isConnected()
except Exception as e:
    logger.info("Unexpected error: {}".format(e))

但是即使 try 应用程序崩溃了...这让我有 2 个选择,

编辑2:

我尝试导入来自 .pyd 的其他模块,认为 .exe 可能找不到它们,但我尝试过的所有 import 都没有任何问题.

编辑3:

太棒了!在查看其他帖子后,我发现还有其他方法可以导入 dll:例如使用 ctypes

问题是:我的 dll 在 C# 中,显然 ctypes 不能很好地支持导入...我遇到了像 WindowsError: [Error -532462766] Windows Error 0xE0434352 这样的错误,这对我没有太大帮助因为这似乎是 windows 中的一个非常普遍的错误,但这让我想到也许在导入 clr 时导致我的程序崩溃的原因是一样的。有谁知道这个错误可能与什么有关?

所以我又开始使用 .NET clr,但还是不行

郑重声明:我已经尝试了 python 32 位和 64 位。这里有更多信息

#with python 32 bits
platform architecture:  ('32bit', 'WindowsPE') 
sys.platform:  win32
os.name:  nt

#with python 64 bits
platform architecture:  ('64bit', 'WindowsPE')
sys.platform:  win32
os.name:  nt

所以我最终成功了:

最终,pythonNET安装正确,但版本似乎不是最好的(不确定是否不是最新的,或者不是正确的版本,或者位),所以我删除了pythonNET的每个文件(那个我在这里和那里复制粘贴以确保它被看到)并下载了最新版本的 .whlhere)(这不是 pip 正在下载的版本)。

最终 import clr 成功了...

我希望这对你们中的一些人有所帮助:)