スポンサーリンク

IronPythonでpyをEXE(.NET Assembly)にコンパイルする。

呼ばれた気がしたので。

現在のIronPythonにはかつてのバージョンにあった「-X:SaveAssenblies」オプションが有りません。ただし、アセンブリにコンパイルできなくなったかというと、そういう事も無くて、pyc.pyというアセンブリ作成のためのスクリプトが用意されています。

pyc.pyは次の深いところにあります。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Program Files (x86)\IronPython 2.7\Tools\Scripts\pyc.py
C:\Program Files (x86)\IronPython 2.7\Tools\Scripts\pyc.py
C:\Program Files (x86)\IronPython 2.7\Tools\Scripts\pyc.py

使い方をまとめます。

まず以下のようなコードがあったとします。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# encoding: shift_jis
print 'はろー'
# encoding: shift_jis print 'はろー'
# encoding: shift_jis
print 'はろー'

これをWindowsのコンソールアプリケーションにコンパイルしたい場合には以下のようにpyc.pyを動かします。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>ipy "C:\Program Files (x86)\IronPython 2.7\Tools\Scripts\pyc.py" /target:exe /main:sample.py sample.py
Input Files:
sample.py
Output:
sample
Target:
ConsoleApplication
Platform:
ILOnly
Machine:
I386
Compiling...
Saved to sample
>ipy "C:\Program Files (x86)\IronPython 2.7\Tools\Scripts\pyc.py" /target:exe /main:sample.py sample.py Input Files: sample.py Output: sample Target: ConsoleApplication Platform: ILOnly Machine: I386 Compiling... Saved to sample
>ipy "C:\Program Files (x86)\IronPython 2.7\Tools\Scripts\pyc.py" /target:exe /main:sample.py sample.py
Input Files:
        sample.py
Output:
        sample
Target:
        ConsoleApplication
Platform:
        ILOnly
Machine:
        I386
Compiling...
Saved to sample

以上のように実行されればsample.exeが作成されているはずです。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>sample
はろー
>sample はろー
>sample
はろー

また、pyc.pyのオプションは以下のようになっています。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pyc: The Command-Line Python Compiler
Usage: ipy.exe pyc.py [options] file [file ...]
Options:
/out:output_file Output file name (default is main_file.)
/target:dll Compile only into dll. Default
/target:exe Generate console executable stub for startup in addition to dll.
/target:winexe Generate windows executable stub for startup in addition to dll.
/? /h This message
EXE/WinEXE specific options:
/main:main_file.py Main file of the project (module to be executed first)
/platform:x86 Compile for x86 only
/platform:x64 Compile for x64 only
Example:
ipy.exe pyc.py /main:Program.py Form.py /target:winexe
pyc: The Command-Line Python Compiler Usage: ipy.exe pyc.py [options] file [file ...] Options: /out:output_file Output file name (default is main_file.) /target:dll Compile only into dll. Default /target:exe Generate console executable stub for startup in addition to dll. /target:winexe Generate windows executable stub for startup in addition to dll. /? /h This message EXE/WinEXE specific options: /main:main_file.py Main file of the project (module to be executed first) /platform:x86 Compile for x86 only /platform:x64 Compile for x64 only Example: ipy.exe pyc.py /main:Program.py Form.py /target:winexe
pyc: The Command-Line Python Compiler

Usage: ipy.exe pyc.py [options] file [file ...]

Options:
    /out:output_file                          Output file name (default is main_file.)
    /target:dll                               Compile only into dll.  Default
    /target:exe                               Generate console executable stub for startup in addition to dll.
    /target:winexe                            Generate windows executable stub for startup in addition to dll.
    /? /h                                     This message

EXE/WinEXE specific options:
    /main:main_file.py                        Main file of the project (module to be executed first)
    /platform:x86                             Compile for x86 only
    /platform:x64                             Compile for x64 only


Example:
    ipy.exe pyc.py /main:Program.py Form.py /target:winexe

IronPythonで動作するPythonコードを.NETのアセンブリにコンパイルしたい場合には、とりあえず多少面倒くさくなってはいますが、元々のIPYのオプションよりは指定できることも多くなっていますので、pyc.pyを試してください。

コメント

タイトルとURLをコピーしました