LoginSignup
14
7

More than 3 years have passed since last update.

複数のバージョンのPythonで使えるPipfileの書き方

Last updated at Posted at 2018-09-30
(An English translation is available here.)

はじめに

pipenv installで生成したPipfileには、Pythonのバージョンの情報が含まれています。このPipfileは、別のバージョンのPythonがインストールされた環境では使うことができません。1 この記事では、Pythonのバージョンによらずに使えるPipfileの書き方を紹介します。

経緯 & 方法

Pipenvを使い始める際に、pipenv installを実行すると、以下のようなPipfileが生成されます。

[[source]]

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


[packages]



[dev-packages]



[requires]

python_version = "3.6"

Python 3.6.4をインストールしたWindows PC上で実行した結果です。

ここで、Python 3.7をインストールした別のWindows PCにこのPipfileを持っていき、pipenv installを実行したとします。すると、次のようなエラーメッセージが表示されます。

Requested Python version (3.6) not installed
Warning: Python 3.6 was not found on your system…
You can specify specific versions of Python with:
  $ pipenv --python path\to\python

pyenvをインストールしたmacOS/Linuxなら、Pipenvが自動的にpyenvを使って必要なバージョンのPythonをインストールしてくれるようですが、残念ながら、pyenvはWindowsには対応していません。

ここで、Pythonのバージョンは特に指定する必要がない場合は、[requires]以下を消去すると、Pythonのバージョンによらず、pipenv installを実行することができるようになります。


[[source]]

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


[packages]



[dev-packages]


この方法は、公式ドキュメントに記載されている方法です。
https://pipenv-ja.readthedocs.io/ja/translate-ja/basics.html#specifying-versions-of-python

The inclusion of [requires] python_version = "3.6" specifies that your application requires this version of Python, and will be used automatically when running pipenv install against this Pipfile in the future (e.g. on other machines). If this is not true, feel free to simply remove this section.

今回の方法を採用する場合は、Pipfile.lockはバージョン管理に含めないほうが良いと思います。Pythonのバージョンごとに異なる内容が出力される場合があるためです。
https://pipenv-ja.readthedocs.io/ja/translate-ja/basics.html#general-recommendations-version-control

  • 複数バージョンのPythonを対象とする場合は、 Pipfile.lock はバージョン管理に含めないでください。

  1. 正確には、major, minor versionが異なる場合は使えません。例えば、Python 3.7をインストールした環境では、3.6が記載されたPipfileを使えません。patch versionのみ異なる場合は使うことができます。例えば、Python 3.6.5をインストールした環境では、3.6.4が記載されたPipfileを使うことができます。 

14
7
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
14
7