pipでのパッケージのインストール先

pip使ってpythonのパッケージをインストールするときに、dnfやapt-getのようにsudoを付けて実行すると警告が表示されますね。

$ sudo pip3 install <package>
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.

--userオプション使えということですが、--user使うとどこにインストールされるのか気になったので調べてみました。

まず、helpから調べてみる。

$ pip3 install --help
...
  --user                      Install to the Python user install directory for
                              your platform. Typically ~/.local/, or
                              %APPDATA%\Python on Windows. (See the Python
                              documentation for site.USER_BASE for full
                              details.)

通常、~/.local/ との事だけど、Pythonドキュメントsite.USER_BASEを読めとあるので探してみる。

site.USER_BASE
Path to the base directory for the user site-packages. Can be None if getuserbase() hasn’t been called yet. Default value is ~/.local for UNIX and Mac OS X non-framework builds, ~/Library/Python/X.Y for Mac framework builds, and %APPDATA%\Python for Windows. This value is used by Distutils to compute the installation directories for scripts, data files, Python modules, etc. for the user installation scheme. See also PYTHONUSERBASE.

site-packagesのベースディレクトリへのパスで、distutilsがパッケージとかをインストールするときに利用すると。通常は~/.localとやはり書いてある。ついでにPYTHONUSERBASEPythonのドキュメントで調べてみる。

PYTHONUSERBASE
Defines the user base directory, which is used to compute the path of the user site-packages directory and Distutils installation paths for python setup.py install --user.

site-packagesのディレクトリを設定できる環境変数のようだ。インストール先を変えたい場合、これを変更すればよさそう。

ちなみにsite-packagesとかuser site-packagesのお話は、PEP370に書いてある。

user site directory

A site directory inside the users' home directory. A user site directory is specific to a Python version. The path contains the version number (major and minor only).