Python における bundler といえば (超ざっくり言えば) virtualenv になるのですが*1、イマイチ使いにくいなと思っていたら pipenv
というものが登場していた。ちょっと使ってみたのでメモ。
何するやつ
Pipfile(.lock)
を元に良い感じに virtualenv 作成 +pip install
してくれるやつPipfile
が使える便利 virtualenv wrapper という感じ
- requests と同じ作者なので流行りそう
インストール
$ pip3 install pipenv
pipenv 環境作成
--three
と指定すると Python3 だけの環境が作られる。--two
も同様。
$ pipenv --three Creating a Pipfile for this project... Creating a virtualenv for this project... ⠋Already using interpreter /usr/bin/python3 Using base prefix '/usr' New python executable in /home/nonylene/.local/share/virtualenvs/pipenv-test-Iy4gNUM5/bin/python3 Also creating executable in /home/nonylene/.local/share/virtualenvs/pipenv-test-Iy4gNUM5/bin/python Installing setuptools, pip, wheel...done. Virtualenv location: /home/nonylene/.local/share/virtualenvs/pipenv-test-Iy4gNUM5
home 以下に virtualenv を作成されたくない場合は環境変数 PIPENV_VENV_IN_PROJECT
を 1 にすれば .venv/
に作成される。この辺 --path
で指定したいですね。
Pipfile
Pipfile
は Gemfile
と同じ要領で使えるもの。toml で記述する。
[[source]] verify_ssl = true url = "https://pypi.python.org/simple"
pip 2.0 に向けて Pipfile
の仮実装があり、これを使っているらしい。
Pipfile
を主に実装した人は pipenv
作った人と同じ人だった。実装は落ち着いているけど pip に取り入れられるのはいつになるのか…。
パッケージ追加
pipenv install [package]
でインストールするとパッケージが virtualenv にインストールされ、 Pipfile
も置き換えてくれる。
$ pipenv install requests Installing requests... Collecting requests Downloading requests-2.13.0-py2.py3-none-any.whl (584kB) Installing collected packages: requests Successfully installed requests-2.13.0 Adding requests to Pipfile's [packages]... P.S. You have excellent taste! ✨ 🍰 ✨
最後の行は作者のライブラリを入れた時に表示される。良い話。
$ cat Pipfile [[source]] verify_ssl = true url = "https://pypi.python.org/simple" [packages] requests = "*"
lock
Pipfile.lock
の生成。
$ pipenv lock Locking [dev-packages] dependencies... ⠹Locking [packages] dependencies... ⠹Updated Pipfile.lock!
Pipfile.lock
が生成される。 json らしい。人間が触るものではない。
$ cat Pipfile.lock { "_meta": { "hash": { "sha256": "da2810af0c3b5333e0de2fce9bea2a228812e2014e5f5fe3b1c533badc6c24e4" }, "requires": {}, "sources": [ { "url": "https://pypi.python.org/simple", "verify_ssl": true } ] }, "default": { "requests": { "hash": "sha256:1a720e8862a41aa22e339373b526f508ef0c8988baf48b84d3fc891a8e237efb", "version": "==2.13.0" } }, "develop": {} }%
activate virtualenv
$ pipenv shell
を用いることで、virtualenv の activate された shell が起動する。
$ pipenv shell Launching subshell in virtual environment. Type 'exit' or 'Ctrl+D' to return. $ python3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> requests.get("http://example.com") <Response [200]>
run
$ pipenv run
を使えば bundle exec
っぽいことができる。これが便利。
$ pipenv run python3 -c "import requests; print(requests.get('http://example.com').text)" <! doctype html> ...
ただ spawn
していて新しいプロセス立ててるので pipe が上手くいってなく、パイプやリダイレクションができない。*2
追記: マージされたので今後は大丈夫になるでしょう。
その他
pipenv install
をするとPipfile.lock
||Pipfile
にもとづいてインストール・ virtualenv 構築してくれる。- その他アップデートコマンドなどもある。 (
README.md
にだいたい書いてある ) - 個人的にはインストールパスを記憶させてほしいけどまだできなさそう
Remember where the virtual environment was created · Issue #252 · kennethreitz/pipenv · GitHub
まとめ
Pipfile をお先に扱える + virtualenv の便利 wrapper として bundler 感覚で使えるのが良いと思いました。積極的に使っていこうと思います。