k-holyのPHPとか諸々メモ

Webで働くk-holyがPHP(スクリプト言語)とか諸々のことをメモしていきます。ソースコードはだいたいWindowsで動かしてます。

Windows7 (MinGW / MSYS) に Python 2.7 + virtualenv + Fabric を入れたメモ

最近、同一環境の複数サーバ(Linux)に対して環境設定することがあって、自動化のためにPython製のツールFabricを使い始めました。

ちょうどそのタイミングで、PHPで作成したWebの管理画面からLAN内の複数のサーバに対してコマンドを実行したいことがありまして、exec()関数でFabricを呼ぶ形にしたらとても便利でした。

それならいっそ、Windowsのローカル開発環境からでも実行できるようにしてみようと思い立ち、Windows7 (MinGW/MSYS) にPythonとFabricを入れたメモです。

Python 2.7 をインストール

Python公式の Download Python から

Pythonの最新安定版は 3.3系の 3.3.1 と 2.7系の 2.7.4 がありますが、3.3系を入れたところ Fabric を入れる段階で詰まってしまいました。

ひとまずWindowsでFabricを使うことが今回の目的なので、とりあえず2.7系にしておきます。

Python 2.7.4 Windows Installer をダウンロードしてインストール。

インストール先はデフォルトの c:\Python27 で。

easy_install と virtualenv をインストール

最近はPHPにも手軽に実行環境を切り替えるphpenvがありますが、Pythonにも virtualenv という同種のツールがあるようで、これをインストールします。

Pythonにはパッケージ管理コマンドとして easy_install と pip があって、pip の方がアンインストールが楽なようですが、とりあえず easy_install で virtualenv を入れて、後は virtualenv から pip を使うという流れで良いようです。

Pythonの環境設定でむかついてる人はとりあえずこれをコピペで実行してください - YAMAGUCHI::weblog

以下、MSYS のシェルで進めます。

$ curl -O http://python-distribute.org/distribute_setup.py
$ /c/Python27/python distribute_setup.py
…中略…
Installing easy_install-script.py script to c:\Python27\Scripts
Installing easy_install.exe script to c:\Python27\Scripts
Installing easy_install-2.7-script.py script to c:\Python27\Scripts
Installing easy_install-2.7.exe script to c:\Python27\Scripts

Installed c:\python27\lib\site-packages\distribute-0.6.36-py2.7.egg
Processing dependencies for distribute==0.6.36
Finished processing dependencies for distribute==0.6.36
After install bootstrap.
Creating c:\Python27\Lib\site-packages\setuptools-0.6c11-py2.7.egg-info
Creating c:\Python27\Lib\site-packages\setuptools.pth

表示されたインストール先に合わせて easy_install コマンドを実行

$ /c/Python27/Scripts/easy_install virtualenv
Searching for virtualenv
Reading http://pypi.python.org/simple/virtualenv/
Reading http://www.virtualenv.org
Reading http://virtualenv.openplans.org
Best match: virtualenv 1.9.1
Downloading http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.1.tar.gz#md5=07e09df0adfca0b2d487e39a4bf2270a
…中略…
Installing virtualenv-script.py script to c:\Python27\Scripts
Installing virtualenv.exe script to c:\Python27\Scripts
Installing virtualenv-2.7-script.py script to c:\Python27\Scripts
Installing virtualenv-2.7.exe script to c:\Python27\Scripts

Installed c:\python27\lib\site-packages\virtualenv-1.9.1-py2.7.egg
Processing dependencies for virtualenv
Finished processing dependencies for virtualenv

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

python -m virtualenv ディレクトリ名 で、指定したディレクトリ名で仮想環境が作成されます。

$ /c/Python27/python -m virtualenv test
New python executable in test\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.

作成されたディレクトリ以下の Scripts/activate.bat を実行します。

$ test/Scripts/activate.bat
test/Scripts/activate.bat: line 1: @echo: command not found
test/Scripts/activate.bat: line 4: syntax error near unexpected token `('
test/Scripts/activate.bat: line 4: `if defined _OLD_VIRTUAL_PROMPT ('

エラーが出力されました。どうもこのバッチファイルはWindowsのコマンドプロンプト用に書かれたもののようです。

ここからは、シェルをMSYSからcmd.exeに変更します。

C:\Users\k_horii\Documents\Projects\>test\Scripts\activate.bat
(test) C:\Users\k_horii\Documents\Projects>

プロンプトが変更されました。

(test) C:\Users\k_horii\Documents\Projects>python --version
python 2.7.4

仮想環境のpythonコマンドも実行可能になりました。

Fabric をインストール

引き続き先ほど作成した仮想環境にて、pipコマンドで Fabric をインストールします。

(test) C:\Users\k_horii\Documents\Projects>pip install fabric
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.

error: Unable to find vcvarsall.bat
----------------------------------------

どうもWindows環境では PyCrypto というモジュールのコンパイル時にエラーになるようです。

英語ですが、ずばりな内容の記事がありましたので、以下これを参考に進めます。

Installing Fabric under Windows 7 64-bit, with virtualenv. - Adam Bard and his magical blog

PyCrypto is a Fabric dependency, and it needs to be compiled via MinGW. To do this, you'll need to edit the file Lib\distutils\distutils.cfg. Add the following lines:

[build] compiler=mingw32

MinGW でコンパイルする必要があるとのことで、仮想環境ディレクトリ内の Lib\distutils\distutils.cfg に上記の設定 [build] compiler=mingw32 を追加します。

You'll also need to mess with your C:\Python27\distutils\cygwincompiler.py. This is in C:\Python27 even for you virtualenv types.

Remove all "-mno-cygwin" flags from lines 322-327 (or wherever the call to set_executables is), so that they look like this:

self.set_executables(compiler='gcc -O -Wall', compiler_so='gcc -mdll -O -Wall', compiler_cxx='g++ -O -Wall', linker_exe='gcc', linker_so='%s %s %s' % (self.linker_dll, shared_option,

記事に従って、 C:\Python27\Lib\distutils\cygwincompiler.py 内の self.set_executables() でコマンドの引数に "-mno-cygwin" と指定されているところを全て削除します。

更に Pywin32 というWindows用拡張ライブラリ?が必要なようです。

Go to http://sourceforge.net/projects/pywin32/ and find the latest 32-bit file for your python from the latest build.

64bit版ではなく32bitの最新版を入れろということです。

Python for Windows extensions - Browse /pywin32 at SourceForge.net

こちらのサイトから現時点の最新32bit版のPython2.7向けファイル pywin32-218.win32-py2.7.exe をダウンロードしました。

また、easy_install コマンドでダウンロードしたファイルへのパスを直接指定する必要があるようです。

(test) C:\Users\k_horii\Documents\Projects>easy_install C:\Users\k_horii\Documents\archives-installed\Python\pywin32-218.win32-py2.7.exe
Processing pywin32-218.win32-py2.7.exe
…中略…
Installing pywin32_postinstall.py script to c:\Users\k_horii\Documents\Projects\test\Scripts
Installing pywin32_postinstall.pyc script to c:\Users\k_horii\Documents\Projects\test\Scripts
Installing pywin32_testall.py script to c:\Users\k_horii\Documents\Projects\test\Scripts
Installing pywin32_testall.pyc script to c:\Users\k_horii\Documents\Projects\test\Scripts

Installed c:\users\k_horii\documents\projects\test\lib\site-packages\pywin32-218-py2.7-win32.egg
Processing dependencies for pywin32==218
Finished processing dependencies for pywin32==218

準備が整ったようなので、Fabricをインストールします。

(test) C:\Users\k_horii\Documents\Projects>pip install fabric
Downloading/unpacking fabric
  Downloading Fabric-1.6.0.tar.gz (215kB): 215kB downloaded
  Running setup.py egg_info for package fabric

    warning: no previously-included files matching '*' found under directory 'docs\_build'
    warning: no previously-included files matching '*.pyc' found under directory 'tests'
    warning: no previously-included files matching '*.pyo' found under directory 'tests'
Downloading/unpacking paramiko>=1.10.0 (from fabric)
  Downloading paramiko-1.10.1.tar.gz (822kB): 822kB downloaded
  Running setup.py egg_info for package paramiko

Downloading/unpacking pycrypto>=2.1,!=2.4 (from paramiko>=1.10.0->fabric)
  Downloading pycrypto-2.6.tar.gz (443kB): 443kB downloaded
  Running setup.py egg_info for package pycrypto

Installing collected packages: fabric, paramiko, pycrypto
  Running setup.py install for fabric
    warning: no previously-included files matching '*' found under directory 'docs\_build'
    warning: no previously-included files matching '*.pyc' found under directory 'tests'
    warning: no previously-included files matching '*.pyo' found under directory 'tests'
    Installing fab-script.py script to c:\Users\k_horii\Documents\Projects\test\Scripts
    Installing fab.exe script to c:\Users\k_horii\Documents\Projects\test\Scripts
    Installing fab.exe.manifest script to c:\Users\k_horii\Documents\Projects\test\Scripts
  Running setup.py install for paramiko

  Running setup.py install for pycrypto
…中略…
Successfully installed fabric paramiko pycrypto
Cleaning up...

少しばかり警告は出てますが、fabric と依存パッケージの paramiko, pycrypto がインストールされたようです。

(test) C:\Users\k_horii\Documents\Projects>fab --version
Fabric 1.6.0
Paramiko 1.10.1

以下の様な内容でテストしてみます。

fabfile.py

from fabric.api import run, env

env.user = 'ユーザー名'
env.password = 'パスワード'

def hostname():
    run('hostname')
(test) C:\Users\k_horii\Documents\Projects>fab -H 192.168.4.130 hostname
[192.168.4.130] Executing task 'hostname'
[192.168.4.130] run: hostname
[192.168.4.130] out: cent6h01
[192.168.4.130] out:


Done.
Disconnecting from 192.168.4.130... done.

実行できました。

cmd.exeではなくMSYSから実行したいところですが、今回はここまでにしておきます。

[追記] virtualenvwrapperを入れてMSYSで実行しました。

Windows7 (MinGW / MSYS) に virtualenvwrapper を入れたメモ

以下のブログ記事を参考にしました。