6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pipfileに本番環境でのみ使うライブラリを指定する

Last updated at Posted at 2019-08-25

Pipfileでは本番環境のみインストールするライブラリを指定することができません。(Gemfileでいうところのgroup: productionが存在しません。)

./Gemfile

# 全環境共通
gem 'rails',        '5.1.6'
~
gem 'jbuilder',     '2.7.0'

# 開発、テスト環境
group :development, :test do
  gem 'sqlite3', '1.3.13'
  gem 'byebug',  '9.0.6', platform: :mri
end

# 開発のみ
group :development do
  gem 'web-console',           '3.5.1'
  ~
end

# 本番のみ
group :production do
  gem 'pg', '0.20.0'
end
./Pipfile

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[requires]
python_version = "3.7"

# 全環境共通
[packages]
psycopg2-binary = "*"
gunicorn = "*"
django-heroku = "*"
django = "*"

# 開発のみ
[dev-packages]
~

# 本番のみ...はない!!

[dev-package]があるのだから[prod-package]があるものだと思っていたけどそうでもないみたいです。gunicornなどは本番でしか使わないので、開発環境ではインストールしたくないです。

Pipfileではなく、pipとrequirements.txtを用いることで分割することもできます(参考記事)。ですがPipenvの他の機能(仮想環境構築など)も捨てがたいので、なんとかしてPipfileの記述だけで本番と開発を分けられないか調べました。

方法

platform_releaseを用いて以下の記述を追記することで、そのライブラリを本番でのみインストールするように指定できます。

"本番のみライブラリ" = {version="*", platform_release ="=='本番platformのリリース情報'"}

本番platformのリリース情報は、本番環境のコンソールでplatform.release()を叩けば返ります。herokuの場合は、

$ heroku run python manage.py shell
Running python manage.py shell on ⬢ sheltered-shelf-12071... up, run.2441 (Free)
Python 3.7.3 (default, Apr  3 2019, 21:35:17) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import platform;platform.release()
'4.4.0-xxxx-aws'

'4.4.0-xxxx-aws'が本番platformのリリース情報です。これを元にPipfileを書き換えます。

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[requires]
python_version = "3.7"

[packages]
psycopg2-binary = {version="*", platform_release ="=='4.4.0-xxxx-aws'"} #本番のみ
gunicorn = {version="*", platform_release ="=='4.4.0-xxxx-aws'"} #本番のみ
django-heroku = "*"
django = "*"

[dev-packages]

この状態で、ローカル(開発環境)でpipenv installをすると、platform_releaseに本番プラットフォーム情報を指定したライブラリに関しては無視されます。


$ pipenv install
Installing dependencies from Pipfile.lock (38deab)
Ignoring gunicorn: markers 'platform_release == "4.4.0-xxxx-aws"' dont match your environment
Looking in indexes: https://pypi.python.org/simple
Ignoring psycopg2-binary: markers 'platform_release == "4.4.0-xxxx-aws"' dont match your environment
Looking in indexes: https://pypi.python.org/simple
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 9/9 — 00:00:01

やや強引な感じもしますが、このようにして本番環境でのみ使うライブラリを指定することができました。

他にうまいやり方をご存知の方がいらっしゃいましたらコメントなどで教えていただけると幸いです。

参考URL

Production-only dependencies #335

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?