2

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:

  • IronPython.dll
  • IronPython.Modules.dll
  • Microsoft.Dynamic.dll
  • Microsoft.Scripting.dll
  • Microsoft.Scripting.Metadata.dll

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

1chenar
  • 197
  • 1
  • 15
  • Can you share a link to that tutorial or give a glimpse of what is in the file? – Ayushya Jul 29 '17 at 18:05
  • @Ayushya this is one link for that. [link](https://dbaportal.eu/2009/12/21/ironpython-how-to-compile-exe/) the file contains some simple file operations (R/W). – 1chenar Jul 29 '17 at 18:08
  • While I was looking into how to solve this I came across [this](https://stackoverflow.com/questions/1578010/ironpython-2-6-py-exe) See if it helps. Till then I am looking into details. – Ayushya Jul 29 '17 at 18:17
  • @Ayushya thanks I saw this post before but nobody mentioned that whats this error message during the compile process. – 1chenar Jul 29 '17 at 18:21
  • [Here](https://mail.python.org/pipermail/ironpython-users/2008-March/006612.html) I looked for the word "Ambiguous" and I did not understand what this means "Method incorrectly casts to PythonFunction." – Ayushya Jul 29 '17 at 18:28
  • @Ayushya thank you. I can't understand too – 1chenar Jul 29 '17 at 18:34
  • I guess you shold look for other options to generate exe file. [This](https://stackoverflow.com/q/3999489/6207775) answer should help you. OR you can wait for someone who has more knowledge about it and can help you. – Ayushya Jul 29 '17 at 18:43
  • @Ayushya thanks for your precious help. I'll check that. – 1chenar Jul 29 '17 at 18:46
  • have you tried to use the compiled version of pyc.py? My installation comes with ipyc.exe with which i can compile my program but wpf doesn't seem to work. I got the error "Error occurred: no module named wpf" – Michael K. Nov 10 '17 at 12:36

1 Answers1

5

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!

Michael K.
  • 2,392
  • 4
  • 22
  • 35
  • Finally a solution that actually works. Thank you so much for posting this. – Jean-Paul Feb 16 '18 at 16:51
  • I copied/pasted the code above and it still does not work for me... I have yet to find a solution for "No module named wpf" error after running the compiled exe... It is ironic how IronPython has seamless .NET integration and yet you cannot distribute your programs.. what good is that? – probat Oct 19 '19 at 18:30