views:

1054

answers:

4

I already attempted using py2exe (not compatible with ipy) and PYC (out of date). Can anyone point me in the direction of a good compiler?

+1  A: 

Check out the IronPython Samples Page

About half way down the page:

Pyc - Python Command-Line Compiler This sample shows developers how to create .NET executables directly out of IronPython scripts. The readme.htm in the download will get you started.

IronPython’s Hosting APIs can be used to compile Python scripts into DLLs, console executables, or Windows executables. The pyc.py script included in this tutorial leverages these hosting APIs and can be used to compile other Python scripts. It provides a variety of flags such as the ability to specify the target platform of the .NET assembly (e.g., x64).

While the assemblies produced by the IronPython Hosting APIs are true .NET assemblies, the dynamic nature of the Python language makes it difficult to use these from other .NET languages. In short, this means that attempting to import Python types into other .NET languages such as C# is not recommended.

Edit: Just noticed that you mentioned PYC was out of date. What makes it so? The IronPython crew seem to still be promoting it, so I would imagine that it's not that far gone.

Toji
Thanks but when I try to run the .exe after PYC compiles it, I get a "string is not callable" error.
Does your python code run if you call it with ipy? That does seem like a strange error. I'll try out some code at work today and see if I can reproduce that problem.
Toji
Yup. My python code works just perfectly if I do "ipy file.py" in command-line, but when its an executable and I call "file.exe" I get the "string is not callable" error.
A: 

Can you provide a minimal repro?

daftspaniel
A: 

I had a bit of trouble trying to implement this solution. This is what I did: 1. Download pyc from http://ironpython.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=423#DownloadId=2364 This took me more searching than it should have because it seems that pyc is hard to find (and I think, a little out of date)

  1. I extracted the pyc folder from the zip file and added it to my IronPython folder in C:\Program Files

  2. Now I tried running this command on the windows console, as instructed by the readme in the pyc download: ipy.exe pyc.py other_hw.py /main:console_hw.py It gave me this error:

=================================

Traceback (most recent call last): File "pyc\pyc.py", line 35, in pyc\pyc.py AttributeError: attribute 'CompilerSink' of 'namespace#' object is read-only

=================================

I made the following change to line 35: Before: class PycSink(Hosting.CompilerSink): After: class PycSink():

Saving the file proved to be a problem due to permissions, so I copied the contents of pyc.py into a new IDLE window (to create a copy), deleted the existing copy of pyc.py and saved the copy as pyc.py in the same location. This takes care of permissions issues and allows changes.

After making this change, I tried running the this command again: ipy.exe pyc.py other_hw.py /main:console_hw.py

However, this time, I got the following error:

=================================

Traceback (most recent call last): File "pyc\pyc.py", line 170, in pyc\pyc.py File "pyc\pyc.py", line 56, in Main AttributeError: attribute 'ResourceFile' of 'namespace#' object is read-only

=================================

At this point, I took stock of the fact that it is now 1 AM and I have a midterm tomorrow, so I undid the changes and shut it down.

Please let me know if you have a solution, or any advancements on mine. Thanks

... To be Continued

inspectorG4dget
+1  A: 

You can use pyc.py, the Python Command-Line Compiler, which is included in IronPython since version 2.6 to compile a Python script to an executable. You find it at %IRONPYTONINSTALLDIR%\Tools\Scripts\pyc.py on your hard disk.

Example

Let's assume you have a simple script test.py that just prints out something to console. You can turn this into an executable with the following command-line (assuming that the IronPython directory is the current directory and that test.py is in there, too):

ipy.exe Tools\Scripts\pyc.py /main:test.py /target:exe

The result will be two files, test.dll and test.exe. test.dll will contain your actual script code, while test.exe is just a launcher for test.dll. You can distribute this EXE and DLL to other computers which do not have IronPython installed if you include the files

  • IronPython.dll,
  • Microsoft.Dynamic.dll,
  • Microsoft.Scripting.Core.dll,
  • Microsoft.Scripting.Debugging.dll,
  • Microsoft.Scripting.dll,
  • Microsoft.Scripting.ExtensionAttribute.dll and
  • IronPython.Modules.dll (sometimes needed).

Also see the blog entry IronPython - how to compile exe.

Christian