スポンサーリンク

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:<span class="hljs-symbol">\P</span>rogram Files (x86)<span class="hljs-symbol">\I</span>ronPython 2.7<span class="hljs-symbol">\T</span>ools<span class="hljs-symbol">\S</span>cripts<span class="hljs-symbol">\p</span>yc.py
C:<span class="hljs-symbol">\P</span>rogram Files (x86)<span class="hljs-symbol">\I</span>ronPython 2.7<span class="hljs-symbol">\T</span>ools<span class="hljs-symbol">\S</span>cripts<span class="hljs-symbol">\p</span>yc.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
<span class="hljs-comment"># encoding: shift_jis</span>
<span class="hljs-keyword">print</span> <span class="hljs-string">'はろー'</span>
<span class="hljs-comment"># encoding: shift_jis</span> <span class="hljs-keyword">print</span> <span class="hljs-string">'はろー'</span>
# encoding: shift_jis
print 'はろー'

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
&gt;ipy <span class="hljs-string">"C:\Program Files (x86)\IronPython 2.7\Tools\Scripts\pyc.py"</span> <span class="hljs-regexp">/target:exe /</span><span class="hljs-string">main:</span>sample.py sample.py
Input <span class="hljs-string">Files:</span>
sample.py
<span class="hljs-string">Output:</span>
sample
<span class="hljs-string">Target:</span>
ConsoleApplication
<span class="hljs-string">Platform:</span>
ILOnly
<span class="hljs-string">Machine:</span>
I386
Compiling...
Saved to sample
&gt;ipy <span class="hljs-string">"C:\Program Files (x86)\IronPython 2.7\Tools\Scripts\pyc.py"</span> <span class="hljs-regexp">/target:exe /</span><span class="hljs-string">main:</span>sample.py sample.py Input <span class="hljs-string">Files:</span> sample.py <span class="hljs-string">Output:</span> sample <span class="hljs-string">Target:</span> ConsoleApplication <span class="hljs-string">Platform:</span> ILOnly <span class="hljs-string">Machine:</span> I386 Compiling... Saved to sample
&gt;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
&gt<span class="hljs-comment">;sample</span>
はろー
&gt<span class="hljs-comment">;sample</span> はろー
&gt;sample
はろー

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<span class="hljs-attribute">pyc</span>: The Command-Line Python Compiler
<span class="vim">Usage: ipy.<span class="hljs-keyword">exe</span> pyc.<span class="hljs-keyword">py</span> [<span class="hljs-keyword">options</span>] <span class="hljs-keyword">file</span> [<span class="hljs-keyword">file</span> ...]
Option<span class="hljs-variable">s:</span>
/<span class="hljs-keyword">ou</span><span class="hljs-variable">t:output_file</span> Output <span class="hljs-keyword">file</span> name (default <span class="hljs-keyword">is</span> main_file.)
/targe<span class="hljs-variable">t:dll</span> Compile <span class="hljs-keyword">only</span> into dll. Default
/targe<span class="hljs-variable">t:exe</span> Generate console <span class="hljs-built_in">executable</span> stub <span class="hljs-keyword">for</span> startup in addition <span class="hljs-keyword">to</span> dll.
/targe<span class="hljs-variable">t:winexe</span> Generate windows <span class="hljs-built_in">executable</span> stub <span class="hljs-keyword">for</span> startup in addition <span class="hljs-keyword">to</span> dll.
/? /h This message
EXE/WinEXE specific option<span class="hljs-variable">s:</span>
/main:main_file.<span class="hljs-keyword">py</span> Main <span class="hljs-keyword">file</span> of the project (module <span class="hljs-keyword">to</span> <span class="hljs-keyword">be</span> executed <span class="hljs-keyword">first</span>)
/platform:x86 Compile <span class="hljs-keyword">for</span> x86 <span class="hljs-keyword">only</span>
/platform:x64 Compile <span class="hljs-keyword">for</span> x64 <span class="hljs-keyword">only</span>
Example:
ipy.<span class="hljs-keyword">exe</span> pyc.<span class="hljs-keyword">py</span> /main:Program.<span class="hljs-keyword">py</span> Form.<span class="hljs-keyword">py</span> /targe<span class="hljs-variable">t:winexe</span>
</span>
<span class="hljs-attribute">pyc</span>: The Command-Line Python Compiler <span class="vim">Usage: ipy.<span class="hljs-keyword">exe</span> pyc.<span class="hljs-keyword">py</span> [<span class="hljs-keyword">options</span>] <span class="hljs-keyword">file</span> [<span class="hljs-keyword">file</span> ...] Option<span class="hljs-variable">s:</span> /<span class="hljs-keyword">ou</span><span class="hljs-variable">t:output_file</span> Output <span class="hljs-keyword">file</span> name (default <span class="hljs-keyword">is</span> main_file.) /targe<span class="hljs-variable">t:dll</span> Compile <span class="hljs-keyword">only</span> into dll. Default /targe<span class="hljs-variable">t:exe</span> Generate console <span class="hljs-built_in">executable</span> stub <span class="hljs-keyword">for</span> startup in addition <span class="hljs-keyword">to</span> dll. /targe<span class="hljs-variable">t:winexe</span> Generate windows <span class="hljs-built_in">executable</span> stub <span class="hljs-keyword">for</span> startup in addition <span class="hljs-keyword">to</span> dll. /? /h This message EXE/WinEXE specific option<span class="hljs-variable">s:</span> /main:main_file.<span class="hljs-keyword">py</span> Main <span class="hljs-keyword">file</span> of the project (module <span class="hljs-keyword">to</span> <span class="hljs-keyword">be</span> executed <span class="hljs-keyword">first</span>) /platform:x86 Compile <span class="hljs-keyword">for</span> x86 <span class="hljs-keyword">only</span> /platform:x64 Compile <span class="hljs-keyword">for</span> x64 <span class="hljs-keyword">only</span> Example: ipy.<span class="hljs-keyword">exe</span> pyc.<span class="hljs-keyword">py</span> /main:Program.<span class="hljs-keyword">py</span> Form.<span class="hljs-keyword">py</span> /targe<span class="hljs-variable">t:winexe</span> </span>
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をコピーしました