VagrantでPython開発環境
今更ながらVagrantでローカル開発用のベースをさっくり作り直したので備忘用ログ
DebianにPython3入れて、pyvenv上でFlaskを実行する
環境
- OS X Yosemite 10.10.3
- Vagrant:1.8.1
- VirtualBox:5.0.18 r106667
インストールは略
Add box & Initialize
$ vagrant box add debian/jessie64 ---(略)--- $ vagrant box list debian/jessie64 (virtualbox, 8.4.0)
boxを追加して
$ vagrant init debian/jessie64
初期化する。VagrantFileができる
VagrantFile編集
まんまだけど、
- boxの指定
- プライベートIPの設定
- 同期フォルダの指定
- プロビジョニング用のスクリプト指定
を行っている
provision/script.sh作成
スクリプト書く
Python3インストール
現在3.5.1が最新
Debian8だとまだPython2がデフォルト
今回は試しにビルドしてインストールしてみた
(パス周りとか危うい気がするが...)
ちなみにPython3がデフォルトになるのもそろそろらしい
Debian 9 (Stretch) または10 (Buster) でPython 3をデフォルトにするべく作業が始まっている。 http://orangain.hatenablog.com/entry/python3-as-default
パスワードプロンプトの省略
MySQL周りでパスワードの入力を求められる。debconf-utilsでこれに対応する
これも開発環境なのでまぁよしということで..
apt-get
適当に使いそうなもの入れる
Hello Vagrant
起動&プロビジョニングして、sshで接続
$ vagrant up ---(略)--- $ vagrant ssh The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Thu May 19 15:31:52 2016 from 10.0.2.2 vagrant@debian-jessie:~$
ログインできた 。諸々の確認&作業
Python
vagrant@debian-jessie:~$ python3 Python 3.5.1 (default, May 18 2016, 18:15:52) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print('Hello Vagrant') Hello Vagrant
pyvenv
vagrant@debian-jessie:~$ pyvenv venv vagrant@debian-jessie:~$ source venv/bin/activate (venv) vagrant@debian-jessie:~$ python -V Python 3.5.1
pip
(venv) vagrant@debian-jessie:~$ pip list pip (7.1.2) setuptools (18.2) You are using pip version 7.1.2, however version 8.1.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command. (venv) vagrant@debian-jessie:~$ pip install --upgrade pip ---(略)--- Successfully installed pip-8.1.2
ネットワーク
(venv) vagrant@debian-jessie:~$ sudo ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:1e:c6:7e inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe1e:c67e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:158984 errors:0 dropped:0 overruns:0 frame:0 TX packets:20658 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:206872864 (197.2 MiB) TX bytes:3140153 (2.9 MiB) eth1 Link encap:Ethernet HWaddr 08:00:27:c6:95:d1 inet addr:192.168.33.10 Bcast:192.168.33.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fec6:95d1/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:54 errors:0 dropped:0 overruns:0 frame:0 TX packets:22 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:8863 (8.6 KiB) TX bytes:1910 (1.8 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
VagrantFileで指定したプライベートネットワークが追加されている
これを通じて、ゲストとホストのみでやり取りする
Flask on Vagrant
Flaskを起動して、ホストからアクセスできるか試す
(venv) vagrant@debian-jessie:~$ pip install flask ---(略)--- (venv) vagrant@debian-jessie:~$ cat src/hello.py from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return "Hello Vagrant!" if __name__ == '__main__': app.run(host='0.0.0.0') (venv) vagrant@debian-jessie:~$ python src/hello.py * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
この状態でブラウザから(http://192.168.33.10:5000/)アクセェェェス!!
「Hello Vagrant!」が表示される
続き
Flask(on Vagrant)でモック開発のための骨組みを作る - nanahara.log