Creating a Pipfile for multiple versions of Python

tomoyukiaota

Tomoyuki Aota

Posted on September 30, 2018

Creating a Pipfile for multiple versions of Python
(A Japanese translation is available here.)

A Pipfile created by pipenv install command has the version number of Python. This Pipfile cannot be used on the machine with a different version of Python. 1 In this article, I'm going to introduce how to create a Pipfile which can be used for multiple versions of Python.

What is the problem?

When we want to start using Pipenv in a repository, the first thing we do is to issue pipenv install command. This creates a Pipfile like this:

[[source]]

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


[packages]



[dev-packages]



[requires]

python_version = "3.6"

Enter fullscreen mode Exit fullscreen mode

This Pipfile is generated on a Windows PC with Python 3.6.4.

Then, if we copy this Pipfile to another PC with Python 3.7 and issue pipenv install, we will encounter the following error message:

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
Enter fullscreen mode Exit fullscreen mode

On macOS/Linux with pyenv, the required version of Python can be installed by Pipenv using pyenv, but pyenv is not available on Windows.

Solution

If there is no need of specifying Python version, we can delete the [requires] section. Then, pipenv install command will be successful regardless of the version of Python installed on the machine.


[[source]]

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


[packages]



[dev-packages]


Enter fullscreen mode Exit fullscreen mode

What I've done here is described in the official document.
https://pipenv.readthedocs.io/en/latest/basics/#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.

When this method is adopted, Pipfile.lock should not be included in version control. This because the content of Pipfile.lock can be different depending on the versions of Python.
https://pipenv.readthedocs.io/en/latest/basics/#general-recommendations-version-control

  • Do not keep Pipfile.lock in version control if multiple versions of Python are being targeted.

  1. To be exact, Pipfile cannot be used when the major or minor version is different. For example, Pipfile for Python 3.6 cannot be used on a machine with Python 3.7. On the other hand, Pipfile can be used when only the patch version is different. For example, Pipfile for Python 3.6.4 can be used on a machine with Python 3.6.5. 

💖 💪 🙅 🚩
tomoyukiaota
Tomoyuki Aota

Posted on September 30, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related