Ubuntu18のaptでインストールしたpip3ではtargetオプションを使ってもExceptionが発生する(Ubuntu18.04)

pip3でtargetオプションを使って特定ディレクトリにライブラリをインストールしたい

$ pip3 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

このpip3はaptでインストールしたもので、バージョンが低くtargetオプションがそもそも存在しないようだ。

$ pip3 install requests -t workspace/
Collecting requests
  Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests)
  Using cached https://files.pythonhosted.org/packages/81/b7/cef47224900ca67078ed6e2db51342796007433ad38329558f56a15255f5/urllib3-1.25.5-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
  Using cached https://files.pythonhosted.org/packages/18/b0/8146a4f8dd402f60744fa380bc73ca47303cccf8b9190fd16a827281eac2/certifi-2019.9.11-py2.py3-none-any.whl
Collecting idna<2.9,>=2.5 (from requests)
  Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl
Installing collected packages: chardet, urllib3, certifi, idna, requests
Exception:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main
  status = self.run(options, args)
  File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 360, in run
  prefix=options.prefix_path,
  File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 784, in install
  **kwargs
  File "/usr/lib/python3/dist-packages/pip/req/req_install.py", line 851, in install
  self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/lib/python3/dist-packages/pip/req/req_install.py", line 1064, in move_wheel_files
  isolated=self.isolated,
  File "/usr/lib/python3/dist-packages/pip/wheel.py", line 247, in move_wheel_files
  prefix=prefix,
  File "/usr/lib/python3/dist-packages/pip/locations.py", line 153, in distutils_scheme
  i.finalize_options()
  File "/usr/lib/python3.6/distutils/command/install.py", line 274, in finalize_options
  raise DistutilsOptionError("can't combine user with prefix, "
distutils.errors.DistutilsOptionError: can't combine user with prefix, exec_prefix/home, or install_(plat)base

解決策

根本的にはpython3の環境管理用の venv で、まるごとPythonの環境を取り替えてしまう方が良さそう。

Ubuntuでは、

sudo apt install python3-venv

でvenvをインストールし、

python3 -m venv /path/to/target/env_dir

で環境を指定する。

cd /path/to/target/env_dir
source bin/activate

で環境を切り替え、

pip install -U pip

でバージョンを上げる。

あとはその環境内であればtargetオプションが使えるpipに変わる。

解決策?

pip install –target オプションがエラーになって泣きそうになったメモ

Install a Python package into a different directory using pip?

一応、 install-purelibでも解決できるのかも?ただし自分が実行したときには一部のライブラリがうまくインストールできなかったのと、venv入れても問題ないローカルの環境だったので、venvでまるごと環境の切り替えをする方法を選択した。

pip install --install-option="--install-purelib=/python/packages" package_name