Pythonの拡張モジュールからexeファイルを作成後、whlファイルに変換し、別の端末でpip installする

前回は、Python3.5 + Visual C++ Build Tools 2015環境で、Pythonの拡張モジュールをビルドしました。

ただ、Vista世代の端末ではVisual Studio 2015系がインストールできないことから、Python3.5系向けの拡張モジュールがビルドできませんでした。

 
そこで今回は、自分でソースコードからビルドし、それをVista世代端末へ渡してインストールしてみます。

流れとしては、Visual C++ Build Tools 2015を入れた端末で

を行い、Vista世代端末で

  • pipを使って、whlファイルからインストール

となります。

 

環境

今回、ビルドする拡張モジュールとしてPyCryptoを使います。

 

ビルド端末

 

インストール端末

 

ビルド端末での作業

Windowsインストーラーの作成

以下を参考に、Windowsインストーラーを作成します。
5. ビルド済み配布物を作成する — Python 3.5.1 ドキュメント

今回はPyCryptoのソースコードGitHubから取得します。

# 環境作る
D:\Sandbox\pycrypto_build>virtualenv -p c:\python35-32\python.exe env
D:\Sandbox\pycrypto_build>env\Scripts\activate
(env) s:\Sandbox\pycrypto_build>pip list
pip (8.1.2)
setuptools (22.0.5)
wheel (0.29.0)

# GitHubのmasterだと別の理由でビルドエラーになる可能性があるため、リリースされている`v2.6.1`ブランチを使う
(env) D:\Sandbox\pycrypto_build>git clone -b v2.6.1 https://github.com/dlitz/pycrypto.git

# ビルド
(env) D:\Sandbox\pycrypto_build>cd pycrypto
(env) D:\Sandbox\pycrypto_build\pycrypto>python setup.py bdist_wininst
...
creating dist
removing 'build\bdist.win32\wininst' (and everything under it)

# ビルドしたファイルの確認(distディレクトリの中)
# D:\Sandbox\pycrypto_build\pycrypto\dist\pycrypto-2.6.1.win32-py3.5.exe ファイルが作成されているはず
(env) D:\Sandbox\pycrypto_build\pycrypto>cd dist
(env) D:\Sandbox\pycrypto_build\pycrypto\dist>dir /b
pycrypto-2.6.1.win32-py3.5.exe

# ビルドだけで、インストールはされていない
(env) D:\Sandbox\pycrypto_build\pycrypto\dist>pip list
pip (8.1.2)
setuptools (22.0.5)
wheel (0.29.0)

 

whlファイルへの変換

Windowsインストーラー(exe)のままでは、pipでインストールできません。
Windows での Python 2.7, 3.4, 3.5 の拡張モジュールビルド環境 - Qiita

 
そこで、Wheelを使ってexeファイルからwhlファイルへと変換します。
Can I install Python windows packages into virtualenvs? - Stack Overflow

# 変換
(env) D:\Sandbox\pycrypto_build\pycrypto\dist>wheel convert pycrypto-2.6.1.win32-py3.5.exe
d:\sandbox\pycrypto_build\env\lib\site-packages\wheel\pep425tags.py:77: RuntimeWarning: Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
  warn=(impl == 'cp')):
d:\sandbox\pycrypto_build\env\lib\site-packages\wheel\pep425tags.py:81: RuntimeWarning: Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect
  warn=(impl == 'cp')):

# ファイルの確認
(env) D:\Sandbox\pycrypto_build\pycrypto\dist>dir /b
pycrypto-2.6.1-cp35-none-win32.whl
pycrypto-2.6.1.win32-py3.5.exe

 

インストール端末での作業

上記で作成したpycrypto-2.6.1-cp35-none-win32.whlを使って、pipでインストールします。

# 環境づくり
D:\Sandbox\pycrypto_install>virtualenv -p c:\python35-32\python.exe env
D:\Sandbox\pycrypto_install>env\Scripts\activate
(env) D:\Sandbox\pycrypto_install>pip list
pip (8.1.2)
setuptools (22.0.5)
wheel (0.29.0)

# ファイルの確認
(env) D:\Sandbox\pycrypto_install>dir /b
env
pycrypto-2.6.1-cp35-none-win32.whl

# PyPIからでは失敗することを確認
(env) D:\Sandbox\pycrypto_install>pip install pycrypto
...
    c:\python35-32\include\pyconfig.h(68): fatal error C1083: Cannot open include file: 'io.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\cl.exe' failed with exit status 2

# まだインストールされていない
(env) D:\Sandbox\pycrypto_install>pip list
pip (8.1.2)
setuptools (22.0.5)
wheel (0.29.0)

# 先ほど作成したwhlファイルからインストール
(env) D:\Sandbox\pycrypto_install>pip install pycrypto-2.6.1-cp35-none-win32.whl
...
Successfully installed pycrypto-2.6.1

# インストールの確認
(env) D:\Sandbox\pycrypto_install>pip list
pip (8.1.2)
pycrypto (2.6.1)
setuptools (22.0.5)
wheel (0.29.0)

 
無事にインストールできました。