LoginSignup
1
0

More than 1 year has passed since last update.

オフライン環境構築 pip編

Last updated at Posted at 2021-08-21

オフライン環境構築 目次へ

概要

オフライン環境で、pipパッケージをインストールするための手順です。

ポイント

手順を説明する前に、まずpipのパッケージの仕組みを説明します。

pipパッケージの種類

実は、pip パッケージは、pythonやOSの種類やバージョンによって何十種類もあります。

たとえば、numpyのパッケージは、以下29種類も提供されています。

Filename, size File type Python version
numpy-1.21.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Wheel cp310
numpy-1.21.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl Wheel cp310
numpy-1.21.2-cp37-cp37m-macosx_10_9_x86_64.whl Wheel cp37
numpy-1.21.2-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl Wheel cp37
numpy-1.21.2-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl Wheel cp37
numpy-1.21.2-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Wheel cp37
numpy-1.21.2-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.whl Wheel cp37
numpy-1.21.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl Wheel cp37
numpy-1.21.2-cp37-cp37m-win32.whl Wheel cp37
numpy-1.21.2-cp37-cp37m-win_amd64.whl Wheel cp37
numpy-1.21.2-cp38-cp38-macosx_10_9_universal2.whl Wheel cp38
numpy-1.21.2-cp38-cp38-macosx_10_9_x86_64.whl Wheel cp38
numpy-1.21.2-cp38-cp38-macosx_11_0_arm64.whl Wheel cp38
numpy-1.21.2-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl Wheel cp38
numpy-1.21.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl Wheel cp38
numpy-1.21.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Wheel cp38
numpy-1.21.2-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl Wheel cp38
numpy-1.21.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl Wheel cp38
numpy-1.21.2-cp38-cp38-win32.whl Wheel cp38
numpy-1.21.2-cp38-cp38-win_amd64.whl Wheel cp38
numpy-1.21.2-cp39-cp39-macosx_10_9_universal2.whl Wheel cp39
numpy-1.21.2-cp39-cp39-macosx_10_9_x86_64.whl Wheel cp39
numpy-1.21.2-cp39-cp39-macosx_11_0_arm64.whl Wheel cp39
numpy-1.21.2-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl Wheel cp39
numpy-1.21.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl Wheel cp39
numpy-1.21.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Wheel cp39
numpy-1.21.2-cp39-cp39-win32.whl Wheel cp39
numpy-1.21.2-cp39-cp39-win_amd64.whl Wheel cp39
numpy-1.21.2-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl Wheel pp37
numpy-1.21.2.zip Source None

これは、いったいどういうことなのでしょうか。

実は、pipにはパッケージング方法が複数定義されており、パッケージを公開する人が任意に選択できるのです。

pythonのコード(.py)をパッケージングする方式には、sdist方式とwheel方式があります。

image.png

出典:https://packaging.python.org/overview/

sdist 方式

sdist方式は、ソースコードをzipファイルで圧縮したファイルを提供する方式です。

sdistパッケージは、pip install実行時に自動的にコンパイルされます。このときコンパイル環境の設定

wheel 方式

wheelパッケージは、コンパイル済のパッケージです。

wheelパッケージの命名規則は次のように定義されています。

{distribution}-{version}(-{build tag})?-{python tag}-{abi tag}-{platform tag}.whl.

  • distribution
    • Distribution name, e.g. 'django', 'pyramid'.
  • version
    • Distribution version, e.g. 1.0.
  • build tag
    • Optional build number. Must start with a digit. A tie breaker if two wheels have the same version. Sort as the empty string if unspecified, else sort the initial digits as a number, and the remainder lexicographically.
  • language implementation and version tag
    • E.g. 'py27', 'py2', 'py3'.
  • abi tag
    • E.g. 'cp33m', 'abi3', 'none'.
  • platform tag
    • E.g. 'linux_x86_64', 'any'.

出典:https://www.python.org/dev/peps/pep-0491/

要するに、wheelはパッケージを作成した環境に依存します。

まとめ

どの種類のパッケージを公開するかは、パッケージ公開者が任意に選択できます。

pip installコマンドは、次の優先順位でインストールするパッケージを選択します。

  1. 実行環境に適したwheelパッケージ
  2. sdistパッケージ

pip downloadの仕組み

pipにはパッケージをインストールせず、ダウンロードだけするサブコマンドpip downloadが用意されています。

このコマンドはデフォルトでは、実行した環境に適したパッケージをダウンロードします。

たとえば、Ubuntu 20.04、python3.8の環境でnumpyをダウンロードすると、

$ pip download -d dest numpy
  Downloading .../numpy/1.21.2/numpy-1.21.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.8 MB)
     |████████████████████████████████| 15.8 MB 92.1 MB/s 
  Saved ./dest/numpy-1.21.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Successfully downloaded numpy

自動的に、numpy-1.21.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whlがダウンロードされます。

異なる実行環境用のパッケージをダウンロードする場合には、オプションで適切に指定する必要が有ります。

たとえば、Mac OSX用のpython2.7のパッケージをダウンロードする場合には以下のようなコマンドを実行します。

python -m pip download \
   --only-binary=:all: \
   --platform macosx-10_10_x86_64 \
   --python-version 27 \
   --implementation cp \
   SomePackage

このとき、Mac OSX用のパッケージがPyPIに存在しない場合、pipコマンドはエラーを返します。

手順

TBD

1
0
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
1
0