1chenar
1chenar

Reputation: 197

IronPython with WPF compile exe pyc error

I developed an application using IronPython 2.7 with WPF using VS2017 that works fine using the ipy command. I want to create an exe file from the project so I used the following command in cmd:

ipy pyc.py /main:IronPython5.py /target:winexe

which XAML file of project including all related DLLs are located in deploy folder but, I get the following error that I can't understand what is means:

Traceback (most recent call last):
  File "pyc.py", line 332, in <module>
  File "pyc.py", line 327, in Main
  File "pyc.py", line 181, in GenerateExe
SystemError: Ambiguous match found.

The Ironpython5.py contains:

import wpf
from System.Windows import MessageBox
from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        self.str1 = ""
        wpf.LoadComponent(self, 'IronPython5.xaml')
    def Button_Click(self, sender, e):
        if self.str1 == "":
            MessageBox.Show("msg1")
        else:
            MessageBox.Show("msg2")
        pass

if __name__ == '__main__':
    Application().Run(MyWindow())

and also the IronPython5.py contains:

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="IronPython5" Height="300" Width="300"> 
       <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="159,238,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window> 

the dll files are:

please help me how to fix this error to generate the exe file.

Upvotes: 2

Views: 1161

Answers (1)

Michael K.
Michael K.

Reputation: 2412

I just got my sample WPF IronPython Project running by following some hints given by this solution but i used the ipyc.exe to compile it which came with my IronPython 2.7.7 installation.

In order to have all dependencies in the final executable it seems, that you have to load them manually.

Note, that this would not run in Visual Studio without the try/except block as those probably already are added by VS

import clr

try:
    clr.AddReferenceToFileAndPath("IronPython.Wpf.dll")
    clr.AddReferenceToFileAndPath('PresentationCore.dll')
    clr.AddReferenceToFileAndPath('PresentationFramework.dll')
    clr.AddReferenceToFileAndPath('WindowsBase.dll')
except:
    pass

from System.Windows import Application, Window

import wpf

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'Wpf_Example.xaml')


if __name__ == '__main__':
    Application().Run(MyWindow())

Then add the dll's to your project folder. They can be found here: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\

And compile with ipyc

ipyc Wpf_Example.py /target:winexe

Note the different way to call ipyc! It won't work if you use the /main: option!

Upvotes: 5

Related Questions