Search code examples
c#python.netcx-freezepython.net

"import clr" issue after fine compilation with cx_freeze


I have a problem with import clr that crashes the .exe of my small program.

Here are the imports I use in my program:

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

Basically the program compile fine and I get an executable thanks to cx_freeze, but when I launch it with a log, it crashes when comes import clr

I don't know what to do to make it work, if anyone has an idea?

EDIT: I added a try-except around the import clr like this:

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

But even with the try the appplication crashes... That leads me with 2 options,

  • either my try except isn't good and then why?
  • there is a real problem with clr and I need help about it.

EDIT2:

I tried importing other module that are from .pyd thinking it might be that the .exe could not find them but all the import I've tried were done without any problem.

EDIT3:

SO! After looking on other posts, I've seen that there were other ways to import dll: by using ctypes for example.

Problem is: my dll is in C# and apparently the import is not supported very well by ctypes... I got errors like WindowsError: [Error -532462766] Windows Error 0xE0434352 which did not help me much since it seems to be a very general error in windows, but this made me think maybe the thing that makes my program crash when importing clr is the same.. Does anyone know what could the error be related to?

So I am back to using .NET clr, but still no luck

For the record: I have tried both python 32 bits and 64 bits. Here are some more information

#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

Solution

  • So I eventually made it work:

    In the end, pythonNET was installed correctly but it seems the version wasn't the best (not sure if not the latest, or not the right version, or bit), so I deleted every file of pythonNET (that I had copy-pasted here and there to be sure it was seen) and downloaded the .whl (here) of a latest version (which wasn't the one pip was downloading).

    And eventually the import clr worked...

    I hope this helps some of you :)