PyPI以外からパッケージをpip installする

最近は仕事の中での運用が楽になるようなpython製の自作ツールを作ってPyPIとかで配布して手軽に使えるようにしたいという気持ちが高まり外部パッケージの配布方法など調べていると、今更ながらPyPI以外(ローカルやGitHub)から直接パッケージをインストールできることを知ったのでメモ。これだと社内でパッケージを配布する時にも使えそう。

環境

今回はこちらのパッケージを対象に様々なパッケージのインストール方法を行っていく。 github.com

PyPIからインストール

特に何も指定せずにpip installするとPyPI(The Python Package Index)からパッケージがダウンロードされる。

$ pip install sampleproject
Collecting sampleproject
  Using cached sampleproject-2.0.0-py3-none-any.whl (4.2 kB)
Collecting peppercorn
  Using cached peppercorn-0.6-py3-none-any.whl (4.8 kB)
Installing collected packages: peppercorn, sampleproject
Successfully installed peppercorn-0.6 sampleproject-2.0.0

pip listでインストール済のパッケージ一覧(管理パッケージも含め)を表示させることができる。

$ pip list
Package       Version
------------- -------
peppercorn    0.6
pip           20.2.4
sampleproject 2.0.0
setuptools    47.1.0

note.nkmk.me

ちなみにパッケージのアンインストールはpip uninstall <Package Name>で行う。

$ pip uninstall sampleproject
Found existing installation: sampleproject 2.0.0
Uninstalling sampleproject-2.0.0:
  Would remove:
    /path/to/venv/bin/sample
    /path/to/venv/lib/python3.8/site-packages/sample/*
    /path/to/venv/lib/python3.8/site-packages/sampleproject-2.0.0.dist-info/*
    /path/to/venv/my_data/data_file
Proceed (y/n)? y
  Successfully uninstalled sampleproject-2.0.0

ソースリポジトリからインストール

<Version Control System>+<Protocol>://<Repository URL>/#egg=<Package Name>の形でソースリポジトリからインストールできる。

Version Control Systemは現状でGit、SubversionMercurial、Bazaarに対応している。

$ pip install git+https://github.com/pypa/sampleproject#egg=sampleproject
Collecting sampleproject
  Cloning https://github.com/pypa/sampleproject to /private/var/folders/z9/54__8rtd2vj7z7z1d27ckcbm0000gn/T/pip-install-t1zs55fp/sampleproject
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Requirement already satisfied: peppercorn in ./venv/lib/python3.8/site-packages (from sampleproject) (0.6)
Building wheels for collected packages: sampleproject
  Building wheel for sampleproject (PEP 517) ... done
  Created wheel for sampleproject: filename=sampleproject-2.0.0-py3-none-any.whl size=4208 sha256=39820d5c160094d41e103e2aeec4d53af0f56e5fdcfab5752d6b76a6249940ae
  Stored in directory: /private/var/folders/z9/54__8rtd2vj7z7z1d27ckcbm0000gn/T/pip-ephem-wheel-cache-hev_ebop/wheels/0d/aa/5e/4788bdead4e57aa3d564a34752934c4fdac2637c2741013624
Successfully built sampleproject
Installing collected packages: sampleproject
Successfully installed sampleproject-2.0.0

ローカルにあるパッケージをインストール

まずはgit cloneリポジトリにあるパッケージをローカルに落とす。

$ git clone https://github.com/pypa/sampleproject.git
Cloning into 'sampleproject'...
remote: Enumerating objects: 490, done.
remote: Total 490 (delta 0), reused 0 (delta 0), pack-reused 490
Receiving objects: 100% (490/490), 121.91 KiB | 312.00 KiB/s, done.
Resolving deltas: 100% (243/243), done.

$ cd sampleproject

カレントディレクトリからインストールする場合は.(ドット)で指定するだけでOK。

-eオプションを使用することで、Editableモードを有効化し、コードの変更が即時に反映されるようになる。

$ pip install -e .
Obtaining file://path/to/sampleproject
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Requirement already satisfied: peppercorn in /path/to/python-plactice/venv/lib/python3.8/site-packages (from sampleproject==2.0.0) (0.6)
Installing collected packages: sampleproject
  Running setup.py develop for sampleproject
Successfully installed sampleproject
(venv) ~/Development/sandbox/python-plactice/sampleproject (master)
$ pip install
ERROR: You must give at least one requirement to install (see "pip help install")

$ pip list
Package       Version Location
------------- ------- ----------------------------------------------------------------------
peppercorn    0.6
pip           20.2.4
sampleproject 2.0.0   /path/to/sampleproject/src
setuptools    47.1.0

参考

https://amzn.to/3oxsJwzamzn.to