(A Japanese translation is available here.)
In this article, I’m going to describe how to create a Pipfile when we have different installation instructions depending on operating systems.
As an actual example, I’m going to create a Pipfile which can be used on both macOS/Windows for PyTorch v0.4.1.
Introduction
For most Python packages, installation instructions are the same regardless of operating systems. For example, numpy
can be installed like this:
pipenv install numpypipenv install numpypipenv install numpy
Enter fullscreen mode Exit fullscreen mode
However, there are some packages which has different installation instructions depending on operating systems. For example, PyTorch v0.4.1 is installed by the following command on macOS:
pipenv install torchpipenv install torchpipenv install torch
Enter fullscreen mode Exit fullscreen mode
On Windows, it is installed like this:
pipenv install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whlpipenv install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whlpipenv install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl
Enter fullscreen mode Exit fullscreen mode
(In this example, I assume the installation of PyTorch is for Python 3.6 without CUDA.)
In Pipfile, we need to let the former instruction run on macOS and let the latter instruction run on Windows. How can we achieve it?
Solution
Specify sys_platform
depending on operating systems. In the official document of Pipenv, the case of installing pywinusb
only on Windows is described.
For PyTorch v0.4.1, the Pipfile I actually use is like this:
[[source]]url = "https://pypi.org/simple"verify_ssl = truename = "pypi"[packages]theano = "*"tensorflow = "*"keras = "*"numpy = "*"scipy = "*"matplotlib = "*"seaborn = "*"jupyter = "*"jupyterlab = "*"ipython = "*"pandas = "*"scikit-learn = "*"spyder = "*"pillow = "*"torch = {version = "==0.4.1", sys_platform = "== 'darwin'"}"b4b7455" = {file = "http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl", sys_platform = "== 'win32'"}torchvision = "*"[dev-packages]pylint = "*"[requires]python_version = "3.6"[[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [packages] theano = "*" tensorflow = "*" keras = "*" numpy = "*" scipy = "*" matplotlib = "*" seaborn = "*" jupyter = "*" jupyterlab = "*" ipython = "*" pandas = "*" scikit-learn = "*" spyder = "*" pillow = "*" torch = {version = "==0.4.1", sys_platform = "== 'darwin'"} "b4b7455" = {file = "http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl", sys_platform = "== 'win32'"} torchvision = "*" [dev-packages] pylint = "*" [requires] python_version = "3.6"[[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [packages] theano = "*" tensorflow = "*" keras = "*" numpy = "*" scipy = "*" matplotlib = "*" seaborn = "*" jupyter = "*" jupyterlab = "*" ipython = "*" pandas = "*" scikit-learn = "*" spyder = "*" pillow = "*" torch = {version = "==0.4.1", sys_platform = "== 'darwin'"} "b4b7455" = {file = "http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl", sys_platform = "== 'win32'"} torchvision = "*" [dev-packages] pylint = "*" [requires] python_version = "3.6"
Enter fullscreen mode Exit fullscreen mode
In [packages]
, there are two lines for PyTorch v0.4.1.
torch = {version = "==0.4.1", sys_platform = "== 'darwin'"}"b4b7455" = {file = "http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl", sys_platform = "== 'win32'"}torch = {version = "==0.4.1", sys_platform = "== 'darwin'"} "b4b7455" = {file = "http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl", sys_platform = "== 'win32'"}torch = {version = "==0.4.1", sys_platform = "== 'darwin'"} "b4b7455" = {file = "http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl", sys_platform = "== 'win32'"}
Enter fullscreen mode Exit fullscreen mode
In these lines, sys_platform = "== 'darwin'"
specifies that this is only for macOS, and sys_platform = "== 'win32'"
specifies that this is only for Windows.
By the way, "b4b7455"
is written on Pipfile when pipenv install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl
is executed on Windows. I added sys_platform = "== 'win32'"
then.
In GitHub, I created a repository, pipenv_cross-platform_example, and put Pipfile and Pipfile.lock there, so this repository may be useful as an example.
暂无评论内容