[Flask]環境構築メモ
はじめに
Flaskはとても軽量なPythonのフレームワークで簡単にAPIを作成したいときなどにファイル1つさえあれば動くようなフレームワークです。Pythonは今まで経験がないので、Python3をインストールするところから説明します。
環境
- macOS Mojave 10.14
手順
- Python3のインストール
- venvモジュールで仮想環境を作る
- 仮想環境を有効化する
- Flaskをインストールする
Python3のインストール
Flaskの公式ではPython3の最新版をおすすめしているのでPython3系をインストールします。
- homebewのインストール
公式にインストールのスクリプトが掲載されているので参考にしてください。
- Python3のインストール
brew install python3
- Python3のバージョン確認
python3 -V
venvで仮想環境構築
プロジェクトの依存環境を管理するためにvenv
というモジュールをいれます。
仮想環境を作りたいディレクトリに移動して下記のコマンドを実行します
python3 -m venv [ここにディレクトリ名]
venvを有効化
Flaskの公式ではvenvを入れたら仮想環境を有効化してくれとあるので有効化します。 venvの公式では以下のような注翻があるので軽く目を通しておきました...
When a venv is active (i.e. the venv’s Python interpreter is running), the attributes sys.prefix and sys.exec_prefix point to the base directory of the venv, whereas sys.base_prefix and sys.base_exec_prefix point to the non-venv Python installation which was used to create the venv. If a venv is not active, then sys.prefix is the same as sys.base_prefix and sys.exec_prefix is the same as sys.base_exec_prefix (they all point to a non-venv Python installation).
有効化するコマンドはシェルによって異なります。
source <venv>/bin/activate
- fish
. <venv>/bin/activate.fish
source <venv>/bin/activate.csh
ちなみに私はfish shellを使っていますが、Flaskの公式にある以下のコマンドはbashのコマンドだったようで
. venv/bin/activate
以下のエラーで若干ハマりました
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then ^ from sourcing file venv/bin/activate called on line 359 of file /usr/local/Cellar/fish/3.0.1/share/fish/config.fish in function '.' called on standard input
fish使ってるのに知識不足です...
Flaskインストール
pip install Flask
参考
http://flask.pocoo.org/docs/1.0/installation/ https://qiita.com/7110/items/1aa5968022373e99ae28 https://docs.python.org/ja/3.5/library/venv.html