zaki work log

作業ログやら生活ログやらなんやら

[Python] オフライン環境でpipする

インターネットに繋がらないホストでpip installするには、パッケージファイル指定インストールかローカルPyPIサーバを用意する。

普通にやっても

[zaki@centos1 ~]$ pip install pypdf2
Collecting pypdf2
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb49849a310>: Failed to establish a new connection: [Errno 101] \xe3\x83\x8d\xe3\x83\x83\xe3\x83\x88\xe3\x83\xaf\xe3\x83\xbc\xe3\x82\xaf\xe3\x81\xab\xe5\xb1\x8a\xe3\x81\x8d\xe3\x81\xbe\xe3\x81\x9b\xe3\x82\x93',)': /simple/pypdf2/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) ...
:
:

当然PyPIリポジトリに繋がらないのでインストールできない。

以下、記事中のプロンプトがcentos1となっていホストがオフライン、managerとなっているのがネットへの接続制限のないオンライン環境のホストです。

オンライン環境でライブラリをダウンロードして手動で

[zaki@manager pip]$ pip download six
Collecting six
  Downloading https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
  Saved ./six-1.14.0-py2.py3-none-any.whl
Successfully downloaded six
You are using pip version 8.1.2, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[zaki@manager pip]$ ls -l six-1.14.0-py2.py3-none-any.whl 
-rw-rw-r--. 1 zaki zaki 10938  3月 18 07:28 six-1.14.0-py2.py3-none-any.whl
[zaki@centos1 pip]$ sudo pip install six-1.14.0-py2.py3-none-any.whl 
Processing ./six-1.14.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.14.0

ローカルPyPIリポジトリを作る

(準備)ライブラリダウンロード

ダウンロードするとこまでは同じ

[zaki@manager pip]$ pip download jinja2
Collecting jinja2
  Downloading https://files.pythonhosted.org/packages/27/24/4f35961e5c669e96f6559760042a55b9bcfcdb82b9bdb3c8753dbe042e35/Jinja2-2.11.1-py2.py3-none-any.whl (126kB)
    100% |████████████████████████████████| 133kB 3.3MB/s 
  Saved ./Jinja2-2.11.1-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from jinja2)
  Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
  Saved ./MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Successfully downloaded jinja2 MarkupSafe
You are using pip version 8.1.2, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[zaki@manager pip]$ ll
合計 160
-rw-rw-r--. 1 zaki zaki 126746  3月 18 07:41 Jinja2-2.11.1-py2.py3-none-any.whl
-rw-rw-r--. 1 zaki zaki  24348  3月 18 07:41 MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
-rw-rw-r--. 1 zaki zaki  10938  3月 18 07:28 six-1.14.0-py2.py3-none-any.whl

pipが使えるホストが無い場合

PyPI本家にwebアクセスしてブラウザでダウンロードするしかないかな?

例えばJinja2であればここから。
サイドメニューの「Download files」からパッケージファイルをダウンロードできる。
(ただしこの場合、依存するパッケージファイルまではわからないので、pip installを試してみて足りないパッケージ名がエラーで表示されるので都度用意する…しかないのかな。スマートなやりかたわからなかった)

pypi.org

(準備)pypiserverの起動

install

[zaki@manager pip]$ sudo pip install pypiserver
[sudo] zaki のパスワード:
Collecting pypiserver
  Downloading https://files.pythonhosted.org/packages/80/b1/76541cbc2bfea31e3429d9b94ea935de438d1e35bca6a8047195a9d4b2be/pypiserver-1.3.2-py2.py3-none-any.whl (75kB)
    100% |████████████████████████████████| 81kB 2.2MB/s 
Installing collected packages: pypiserver
Successfully installed pypiserver-1.3.2
You are using pip version 8.1.2, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

起動 (パッケージファイルを置いているディレクトリを引数に指定)

[zaki@manager pip]$ pypi-server 
Error: while trying to list root(/home/zaki/packages): [Errno 2] No such file or directory: '/home/zaki/packages'
[zaki@manager pip]$ pypi-server -p 25080 .

こうすると、プライベート環境で25080/TCPでListenするローカルPyPIサーバが起動する。

リモートからHTTPアクセスすると、使用方法が表示される。(Xが無い場合脳内HTMLパーサで変換するか可能ならlynxとか入れてみるとか)

[zaki@centos1 pip]$ curl http://172.29.0.10:25080
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Welcome to pypiserver!</title>
  </head>
  <body>
    <h1>
      Welcome to pypiserver!
    </h1>
    <p>
      This is a PyPI compatible package index serving 2 packages.
    </p>
    <p>
      To use this server with <code>pip</code>, run the following command:
      <pre>
        <code>pip install --index-url http://172.29.0.10:25080/simple/ PACKAGE [PACKAGE2...]</code>
      </pre>
    </p>
    <p>
      To use this server with <code>easy_install</code>, run the following command:
      <pre>
        <code>easy_install --index-url http://172.29.0.10:25080/simple/ PACKAGE [PACKAGE2...]</code>
      </pre>
    </p>
    <p>
      The complete list of all packages can be found <a href="/packages/">here</a> or via the <a href="/simple/">simple</a> index.
    </p>
    <p>
      This instance is running version 1.3.2 of the <a href="https://pypi.org/project/pypiserver/">pypiserver</a> software.
    </p>
  </body>
</html>

ローカルPyPIからのpipインストール

usage通り実行すると

[zaki@centos1 pip]$ pip install --index-url http://172.29.0.10:25080/simple/ jinja2
Collecting pypdf2
  The repository located at 172.29.0.10 is not a trusted or secure host and is being ignored. If this repository is available via HTTPS it is recommended to use HTTPS instead, otherwise you may silence this warning and allow it anyways with '--trusted-host 172.29.0.10'.
  Could not find a version that satisfies the requirement pypdf2 (from versions: )
No matching distribution found for pypdf2

insecureなエラーになってしまう。。
この場合は、pipのオプションの--trusted-hostを指定する。

--trusted-host Mark this host as trusted, even though it does not have valid or any HTTPS.

[zaki@centos1 pip]$ sudo pip install --trusted-host 172.29.0.10 --index-url http://172.29.0.10:25080/simple/ jinja2
Collecting jinja2
  Downloading http://172.29.0.10:25080/packages/Jinja2-2.11.1-py2.py3-none-any.whl (126kB)
    100% |████████████████████████████████| 133kB 124.9MB/s 
Collecting MarkupSafe>=0.23 (from jinja2)
  Downloading http://172.29.0.10:25080/packages/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: MarkupSafe, jinja2
Successfully installed MarkupSafe-1.1.1 jinja2-2.11.1
[zaki@centos1 pip]$ 

インストールできました。


参考

qiita.com

qiita.com


この辺とコンボしてね

zaki-hmkc.hatenablog.com