Djangoアプリケーション開発(1) 環境構築
6月上旬に参加したpython講座の学習内容忘備録。
普段は仕事でPHPの業務アプリケーション開発を行っているんだけれど、そろそろ仕事の幅を広げたいという欲求と、昨今の機械学習やAIブームの盛り上がりでPython言語に興味が湧いてきたため、今の自分の知識と技術力で最も学習効果が高いと思われるPython学習を探して参加してみた。
講座はPython初心者がDjangoによるアプリケーション開発からデプロイまでの一連の作業内容を学習できる内容となっている。
学習内容を大別してリストアップしてみる。
- Pythonの基礎知識
- Vagrantによる仮想環境構築
- DjangoによるWebアプリケーション開発
- D3.jsによるデータの可視化
- RabbitmqとCeleryによる並列処理
- Ansibleによるプロビジョニングとデプロイ
まずはVagrantで立ち上げた仮想環境で、Python環境をデプロイ。
開発環境として、ボックスファイルはubuntu/xenial64を使用する。
app.vagrantup.com
ヴァージョン情報:
Python 3.5.2
Django 1.11.2
PostgreSQL 9.5
目次
- リポジトリ新規作成
- Vagrant初期設定
- 仮想環境起動
- 仮想環境にSSHアクセス
- ubuntuのアップデート
- ミドルウェアのインストール
- プロジェクトのディレクトリに移動
- 仮想環境のプロジェクトディレクトリに virtualenv でpython開発環境を作成
- 開発サーバにパスを通してサーバ立ち上げ
1. リポジトリ新規作成
Githubにプロジェクト用の新規リポジトリを作成、localにclone。
$ cd /path/to/project
$ git clone git@github.com:dsonoda/project_name.git
2. Vagrant環境の初期化
projectのroot直下にVagrantfileを新規作成。
$ cd /path/to/project
$ vagrant init ubuntu/xenial64
実行後のディレクトリ構成
/path/to/project
+/.git
+/.gitignore
+/LICENSE
+/README.md
+/Vagrantfile <= 新規作成される
Vagrantfileの編集
$ vi Vagrantfile
IP設定。line35辺り。以下をコメントアウト。
config.vm.network "private_network", ip: "192.168.33.10"
VirgulaBoxの仮想環境で使用するメモリ設定。
line57辺り。以下をコメントアウトし、任意のメモリを記載。
config.vm.provider "virtualbox" do |vb|
vb.memory = “1024"
end
3. 仮想環境起動
Vagrantfileと同階層で以下を実行。
$ vagrant up
4. 仮想環境にSSHアクセス
Vagrantfileと同階層で以下を実行すると、立ち上がっている仮想環境にアクセスできる。
$ vagrant ssh
ubuntu@ubuntu-xenial:~$
この時点でのミドルウェアを確認してみる。
ubuntu@ubuntu-xenial:~$ dpkg -l
すでにpython3とgitはすでにインストールされているようだ。
仮想環境からサインアウトする場合はVagrantfileと同階層で以下を実行。
ubuntu@ubuntu-xenial:~$ exit
$
5. ubuntuのアップデート
ubuntu@ubuntu-xenial:~$ sudo apt-get update
6. 仮想環境にミドルウェアとツールをインストール
ubuntu@ubuntu-xenial:~$ sudo apt-get install -y python3-pip
python3-pip
はpython3のパッケージマネージャ。
あるモジュールをインストールするために必要な他のモジュールも自動的にインストールしてくれる。
パッケージのインストールとアンインストールコマンドは以下。
pip3 install package_name
pip3 uninstall package_name
pipでインストールされているパッケージの一覧は以下コマンドで確認できる。
pip3 list
ある環境にpipでインストールされているパッケージをまとめて他の環境でインストールしたい場合、freezeオプションでリストを出力する方法が便利。
Ansibleなどでのプロビジョニングスクリプトを組む場合はたいていこの方法でパッケージインストールをする。
pip3 freeze > requirements.txt
pip3 install -r requirements.txt
例)requirements.txt には以下の形式でパッケージリストが出力される。
cryptography==1.9
Django==1.11.2
django-bootstrap3==8.2.3
numpy==1.13.0
pandas==0.20.2
pyOpenSSL==17.1.0
python-dateutil==2.6.0
Scrapy==1.4.0
celery==4.0.2
psycopg2==2.7.1
python-devとpython3-setuptoolsは、python3-pip
インストール時に依存パッケージとして同時にインストールされるようである。python3-pip
インストール後には以下コマンドで確認できる。
ubuntu@ubuntu-xenial:~$ dpkg -l python3-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-===================-==============-==============-===========================================
ii python3-dev 3.5.1-3 amd64 header files and a static library for Pytho
ubuntu@ubuntu-xenial:~$ dpkg -l python3-setuptools
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-===================-==============-==============-===========================================
ii python3-setuptools 20.7.0-1 all Python3 Distutils Enhancements
また、アプリケーション開発でDBはPostgreSQL
を使用するためついでにインストールしている。
python-psycopg2
は、PythonからPostgreSQLに接続するためのドライバモジュール。
ubuntu@ubuntu-xenial:~$ sudo apt-get install -y postgresql-9.5
ubuntu@ubuntu-xenial:~$ sudo apt-get install -y python-psycopg2
virtualenvもインストール。
ubuntu@ubuntu-xenial:~$ sudo pip3 install virtualenv
入らない時はこちらを実行する。
ubuntu@ubuntu-xenial:~$ sudo -- sh -c 'export LC_ALL=C; pip3 install virtualenv'
7. プロジェクトのディレクトリに移動
ubuntu@ubuntu-xenial:~$ cd /vagrant
仮想環境実行時は、ローカル環境のVagrantfileがある階層と仮想環境の/vagrantディレクトリは常に同期されている。
ローカル環境で編集・Git管理されたソースコードは、自動的に仮想環境に反映される。
ローカル環境:/path/to/project
仮想環境:/vagrant
8. 仮想環境のプロジェクトディレクトリに virtualenv でpython開発環境を作成
virtualenvを用いる。開発するアプリケーション毎に環境を用意すると不要なモジュールのないシンプルな環境で毎回開発が可能になる。
“venv"という名前で開発環境作成。
ubuntu@ubuntu-xenial:/vagrant$ virtualenv venv
実行後のディレクトリ構成
/vagrant
+/.git
+/.gitignore
+/LICENSE
+/README.md
+/.vagrant
+/Vagrantfile
+/venv <= 新規作成される
Python3バージョンを指定する場合
ubuntu@ubuntu-xenial:/vagrant$ virtualenv -p /usr/bin/python3 venv
9. Python開発環境にパスを通す・パスを抜ける
パスを通す場合
ubuntu@ubuntu-xenial:/vagrant$ source venv/bin/activate
(venv) ubuntu@ubuntu-xenial:/vagrant$
パスから抜ける場合
(venv) ubuntu@ubuntu-xenial:/vagrant$ deactivate
ubuntu@ubuntu-xenial:/vagrant$