--- lang: ja-jp tags: Python --- # Python 依存関係の管理 - [Python モジュールのインストール](https://docs.python.org/ja/3/installing/index.html) - [PyPA >> Python Packaging User Guide >> Tutorials >> Managing Application Dependencies](https://packaging.python.org/tutorials/managing-dependencies/) ## TL;DR - PyPI (Python Packaging Index) : 一般公開されるパッケージのリポジトリ - PyPA (Python Packaging Authority) : パッケージに関連する種々の仕様策定を行う組織 1. Python のパスを調査する( bin と lib/python3.x/site-packages へのパスを調べる ) 2. CI 環境などでパッケージのキャッシングを利用する場合には `1.` のパスとその他独自に設定する仮想環境を対象とすれば良い ### pip install --user > Install to the Python user install directory for your platform. > Typically `~/.local/`, or `%APPDATA%\Python` on Windows. (See the Python documentation for site.USER_BASE for full details.) Linux, Unix, macOS では `~/.local` というディレクトリにパッケージをインストールする。 macOS を利用しているので、 `ls -l ~/.local` を実行すると以下のような結果を得る: ```shell= % ls -l ~/.local total 0 drwxr-xr-x 3 user staff 96 12 10 2019 bin drwxr-xr-x 3 user staff 96 7 22 2019 lib drwxr-xr-x 4 user staff 128 4 10 2019 share ``` このディレクトリの `lib` 配下の対応する Python バージョンの元に `site-packages` が存在しており、各種パッケージがインストールされていることが確認できる。 ```shell= % ls -l ~/.local/lib/python3.6/site-packages total 24 drwxr-xr-x 3 user staff 96 7 22 2019 __pycache__ drwxr-xr-x 14 user staff 448 7 22 2019 isort ... ``` #### installation via venv > Note that the --user flag has no effect when inside a virtual environment - all installation commands will affect the virtual environment. 仮想環境が有効化されている状況下においては、 `--user` オプションを追加した `pip install` を行ったとしてもその効果はなく、ユーザにより所有される仮想環境にパッケージがインストールされることになる。 ```shell= % source venv/bin/activate # or % source venv/bin/activate.fish % ls -l venv/lib/python3.6/site-packages | head -n 5 total 89936 -rwxr-xr-x 1 user staff 44711884 4 9 17:13 56fa58acf89604978a41__mypyc.cpython-36m-darwin.so drwxr-xr-x 25 user staff 800 4 9 17:14 IPython drwxr-xr-x 8 user staff 256 4 9 17:14 Keras_Applications-1.0.8.dist-info drwxr-xr-x 8 user staff 256 4 9 17:14 Keras_Preprocessing-1.1.0.dist-info ``` ### Caching in CircleCI [CircleCI >> 依存関係のキャッシュ](https://circleci.com/docs/ja/2.0/language-python/#%E4%BE%9D%E5%AD%98%E9%96%A2%E4%BF%82%E3%81%AE%E3%82%AD%E3%83%A3%E3%83%83%E3%82%B7%E3%83%A5) CircleCI のジョブ・ワークフローを構築する際に、依存パッケージのキャッシュを利用した高速化を実現したい場合がある。そのような場合に「何を対象として」キャッシュを行うべきかを整理する。 ジョブの実行環境は次の通り: ```yaml= version: 2 jobs: build: working_directory: ~/circleci-demo-python-django docker: - image: circleci/python:3.6.4 environment: PIPENV_VENV_IN_PROJECT: true ... ``` [pipenv >> 仮想環境の独自の配置場所](https://pipenv-ja.readthedocs.io/ja/translate-ja/advanced.html#custom-virtual-environment-location)に説明がある通り、 `PIPENV_VENV_IN_PROJECT` 環境変数を設定することにより `project/.venv` のパスに仮想環境を構築することが可能となっている。 ```yaml= version: 2 jobs: build: # ... steps: - checkout - run: sudo chown -R circleci:circleci /usr/local/bin - run: sudo chown -R circleci:circleci /usr/local/lib/python3.6/site-packages - restore_cache: # このステップは依存関係をインストールする*前*に実行します key: deps9-{{ .Branch }}-{{ checksum "Pipfile.lock" }} - run: command: | sudo pip install pipenv pipenv install - save_cache: key: deps9-{{ .Branch }}-{{ checksum "Pipfile.lock" }} paths: - ".venv" - "/usr/local/bin" - "/usr/local/lib/python3.6/site-packages" ``` 今回の例では `circleci/python:3.6.4` イメージの Python のバイナリと site-packages を `pipenv` のキャッシュの対象に、 `.venv` を依存パッケージのインストール先としてキャッシュの対象に設定している。この設定により再度 Python の環境構築を CircleCI の実行時に行う際に、パッケージのインストールの手順(の一部:ダウンロードなど)をスキップすることができ、高速に動作するようになる。 ジョブを実行するのは `circleci:circleci` というユーザであり、 Docker イメージ内では root が `/usr/local/bin` などのパスを所有しているため `chown` による権限の付与が行われていることに注意する。