Information in this document is subject to change without notice. The example companies, organizations, products, people, and events depicted herein are fictitious. No association with any real company, organization, product, person or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarked, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.
© Microsoft Corporation. All rights reserved.
Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active Directory, BizTalk, SQL Server, SharePoint, Outlook, PowerPoint, FrontPage, Visual Basic, Visual C++, Visual J++, Visual InterDev, Visual SourceSafe, Visual C#, Visual J#, and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
Other product and company names herein may be the trademarks of their respective owners.
This sample is subject to terms and conditions of the Shared Source License for IronPython. A copy of the license can be found in the License.html file at the root of this distribution. If you can not locate the Shared Source License for IronPython, please send an email to ironpy@microsoft.com. By using this sample in any fashion, you are agreeing to be bound by the terms of the Shared Source License for IronPython.
Python Command Line Compiler
Prerequisites
The prerequisites to successfully run this application are:
Getting Started
Navigate to the Pyc sample directory and copy the IronPython DLLs (IronPython.dll and IronMath.dll) here. The compiled executables require the presence of IronPython.dll and IronMath.dll in the current directory. If not found by the .NET runtime, the compiled executables will fail with "System.IO.FileNotFoundException: Could not load file or assembly 'IronPython, Version, ...".
Please make sure the folder structure in the Pyc directory is:
.\console_hw.py
.\IronMath.dll
.\IronPython.dll
.\other_hw.py
.\pyc.py
.\readme.htm
.\winforms_hw.py
Overview
This sample demonstrates the use of the Python Hosting APIs to compile Python files into a .NET executable. The behavior is essentially a command line equivalent of the IronPython Visual Studio integration sample released with the Visual Studio SDK. (The complete Visual Studio SDK integration sample can be downloaded from here.)
For the list of command line options, run:
ipy.exe pyc.py
Command Line Options
The format of the command line for running pyc.py is as follows:
ipy.exe pyc.py [options] file [file ...]
The available options are:
/? or /h
Command line help
/out:output_file
The name of the output file. If omitted, the name of the last
input file specified (with extension dll or exe) will be used for
the output file.
/main:main_file.py
The main module of the executable. This is the module whose code will be
executed first in the compiled executable. If /out is omitted, the name of
the output assembly will be: main_file.<extenstion>
/target:(dll|exe|winexe)
The target assembly type. DLL, console executable (exe), or windows
executable (winexe). Default is "exe".
/r:assembly or /reference:assembly
Adds a reference to the .NET assembly to the compiled code. This allows the
Python code to reference .NET assemblies without ever using the "AddReference" function of the clr module. Example: /r:System.Windows.Forms
/platform:(x86|x64|Itanium)
Optional argument to specify/restrict the target platform of the .NET
assembly. Specifying x86 will set assembly requirement to 32bit execution
only. Default setting is to produce a platform independent .NET assembly.
/res:rfile.resource[,(public|private)]
Include public or private resource file (*.resource) in the assembly. The
resource type can be set to public or private with the optional modifier
following a comma.
Examples
ipy.exe pyc.py /main:console_hw.py
Compiles console_hw.py into console executable console_hw.exe.
ipy.exe pyc.py other_hw.py /main:console_hw.py
Compiles two input files (other_hw.py and console_hw.py) into console_hw.exe. Contents
of console_hw.py will be executed directly.
ipy.exe pyc.py /main:winforms_hw.py /r:System.Windows.Forms /target:winexe
Compiles winforms_hw.py into winforms_hw.exe and includes a reference to System.Windows.Forms. winforms_hw.py
can then include code such as:
import System.Windows.Forms
form = System.Windows.Forms.Form(Text = "Hello")
form.ShowDialog()
Notes
The compiled executable requires the presence of IronPython.dll and IronMath.dll in the same directory from which the compiled assembly is being executed. If IronPython.dll is not present in the current directory, the following exception will be raised:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'IronPython, ..."