環境
環境は以下の通りです。Vagrantはなくても本来的には差支えないですが、本稿ではVagrantを利用する前提で進めます。
ホスト側
- Windows 8.1
- VirtualBox 4.3
- Vagrant 1.6
VM側
- Ubuntu 13.04
- Python 2.7
まずはVagrantをinit
Vagrantfileを保存したいディレクトリでvagrant initとvagrant upします。
initするときは事前にaddしてあるboxを指定してください。私の場合は、以前addしたubuntu13のboxを指定してます。
$ vagrant init ubuntu13
# でもってSSH
$ vagrant sshvirtualenvのインストール
試しにenvという環境を構築します。発行コマンドは以下(テンプレ的ですが)。
# setuptoolのインストールスクリプトをDL
$ wget http://peak.telecommunity.com/dist/ez_setup.py
# setuptoolをインストール
$ sudo python ez_setup.py
# virtualenvをインストール
$ sudo easy_install virtualenv
# virtualenvの環境構築
$ sudo virtualenv --no-site-packages envvirtualenv内でPyramid環境構築
出来上がったvirtualenv内でPyramidをインストールします。
$ cd env
$ bin/easy_install pyramid注意したいのは、実行するeasy_installは飽くまでvirtualenv内にインストールしたeasy_installという点です。ここで誤ってeasy_install pyramidとするとグローバルな環境にPyramidがインストールされてしまい、他の環境へ干渉してしまいます。
実行すると以下のメッセージが出ます。
Searching for pyramid
Reading https://pypi.python.org/simple/pyramid/
Best match: pyramid 1.5.1
Downloading https://pypi.python.org/packages/source/p/pyramid/pyramid-1.5.1.tar.gz#md5=8a1ab3b773d8e22437828f7df22852c1
Processing pyramid-1.5.1.tar.gz
Writing /tmp/easy_install-kqIOmK/pyramid-1.5.1/setup.cfg
Running pyramid-1.5.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-kqIOmK/pyramid-1.5.1/egg-dist-tmp-hGqkdb
Adding pyramid 1.5.1 to easy-install.pth file
Installing ptweens script to /home/vagrant/env/bin
Installing pdistreport script to /home/vagrant/env/bin
Installing proutes script to /home/vagrant/env/bin
Installing pshell script to /home/vagrant/env/bin
Installing prequest script to /home/vagrant/env/bin
Installing pviews script to /home/vagrant/env/bin
Installing pcreate script to /home/vagrant/env/bin
Installing pserve script to /home/vagrant/env/bin
Installed /home/vagrant/env/lib/python2.7/site-packages/pyramid-1.5.1-py2.7.egg
(中略)
no previously-included directories found matching '*.pyc'
no previously-included directories found matching '*.pyo'
Adding WebOb 1.4 to easy-install.pth file
Installed /home/vagrant/env/lib/python2.7/site-packages/WebOb-1.4-py2.7.egg
Finished processing dependencies for pyramidプロジェクトの作成
Pyramidをインストールしたらプロジェクトを作成します。ここではmy-projectというプロジェクトを作成します。
$ bin/pcreate -s alchemy my-project
$ cd my-projects
$ sudo ../bin/python setup.py develop誤ってpython setup.py developとしないよう気を付けてください。そうすると(先ほどのPyramidインストール時でも述べましたが)virtualenv内のpythonではなくて、その外のpythonによってスクリプトが実行され、グローバルな環境の扱いになります。
ちなみに正しく実行すると以下のようなメッセージが出ます。
Running SQLAlchemy-0.9.7/setup.py -q bdist_egg --dist-dir /tmp/easy_install-D4U7Go/SQLAlchemy-0.9.7/egg-dist-tmp-Erulc6
warning: no files found matching '*.jpg' under directory 'doc'
warning: no files found matching 'distribute_setup.py'
warning: no files found matching 'sa2to3.py'
warning: no files found matching 'ez_setup.py'
no previously-included directories found matching 'doc/build/output'
lib/sqlalchemy/cextension/processors.c:10:20: fatal error: Python.h: No such file or directory
(中略)
Using /usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg
Searching for zope.interface==4.0.5
Best match: zope.interface 4.0.5
Adding zope.interface 4.0.5 to easy-install.pth file
Using /usr/lib/python2.7/dist-packages
Finished processing dependencies for my-project==0.0warningは対応しなくても大丈夫です。
DB初期化
今回、そこまで設定してないけど初期化しないとエラーになるので。
../bin/initialize_my-project_db development.iniもしここで
../bin/initialize_my-project_db: No such file or directoryと出てしまったら、プロジェクト作成の時にsudo ../bin/python setup.py developをsudo /python setup.py developとしていないか確認してください。
サーバー立ち上げ
../bin/pserve development.ini --reload実行メッセージ
Starting subprocess with file monitor
Starting server in PID 2663.
serving on http://0.0.0.0:6543よくある間違い
Pyramidのプロジェクトを作成するときに
$ ../bin/python setup.py develop ではなくて
$ sudo python setup.py developとすると
../bin/pserve development.iniしたときに以下のエラーが出ます。
File "/home/vagrant/env/local/lib/python2.7/site-packages/pkg_resources.py", line 742, in require
needed = self.resolve(parse_requirements(requirements))
File "/home/vagrant/env/local/lib/python2.7/site-packages/pkg_resources.py", line 639, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: my-projectもしくは以下のようなエラー。
File "/home/vagrant/env/local/lib/python2.7/site-packages/pkg_resources.py", line 742, in require
needed = self.resolve(parse_requirements(requirements))
File "/home/vagrant/env/local/lib/python2.7/site-packages/pkg_resources.py", line 639, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: waitlessこの場合はプロジェクトの作成からやり直してください。
ブラウザからPyramid見えるようにする
Vagrantの設定を変更して、ホスト側(windows側)からPyramidのローカル環境を見れるようにします。 以下をVagrantfileに追記します。
config.vm.network :forwarded_port, guest: 6543, host: 6543上記の設定ではguestがVM上のポートで、hostの方がホスト側のポートです。設定を追記したらVMを立ち上げなおします。
$ vagrant halt
$ vagrant upアクセスしてみる
では、ホスト側のPCからVM上のPyramidを確認してみましょう。ブラウザからlocalhost:6543にアクセスしてみてください。

以上です。
参考
- http://docs.pylonsproject.jp/projects/pyramid-doc-ja/en/latest/tutorials/wiki2/installation.html#id4
- http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/narr/project.html
- http://docs.pylonsproject.jp/projects/pyramid-doc-ja/en/latest/narr/install.html
- http://o-tomox.hatenablog.com/entry/2013/07/19/143927
- http://stackoverflow.com/questions/13658930/why-am-i-getting-distributionnotfound-error-when-i-try-to-run-pyramid-project
- http://dqn.sakusakutto.jp/2014/02/vagrant_port_forward_80_80.html
- http://pylog.applifirst.jp/2013/10/27/about_pyramid_alchemy/