以编程方式确定pip的“用户安装”位置脚本目录
如pip文档中所述,用户可以使用来在其个人帐户中安装软件包pip install --user <pkg>
。
如何以编程方式确定这样安装的脚本的用户安装位置?我说的是应该添加到PATH的目录,以便可以从命令行调用已安装的软件包。
例如,在Windows中,安装时pip install -U pylint
--user
会收到以下警告,因为'C:\Users\myusername\AppData\Roaming\Python\Python37\Scripts'
我的PATH中没有:
...
Installing collected packages: wrapt, six, typed-ast, lazy-object-proxy, astroid, mccabe, isort, colorama, toml, pylint
Running setup.py install for wrapt ... done
WARNING: The script isort.exe is installed in 'C:\Users\myusername\AppData\Roaming\Python\Python37\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The scripts epylint.exe, pylint.exe, pyreverse.exe and symilar.exe are installed in 'C:\Users\myusername\AppData\Roaming\Python\Python37\Scripts' which is not on PATH.
我是否可以使用一些python代码以编程方式确定该位置(将在Windows / Linux / Darwin / etc下运行)?就像是:
def get_user_install_scripts_dir():
...
# would return 'C:\Users\myusername\AppData\Roaming\Python\Python37\Scripts'
# on Windows with Python 3.7.x, '/home/myusername/.local/bin' in Linux, etc
return platform_scripts_dir
作为备用,是否可以运行一些命令来获取此位置?类似于(但对于脚本位置,不是网站的基本目录):
PS C:\Users\myusername\> python -m site --user-base
C:\Users\myusername\AppData\Roaming\Python
$ python -m site --user-base
/home/myusername/.local
-
我相信以下应该能给出预期的结果
import os import sysconfig user_scripts_path = sysconfig.get_path('scripts', f'{os.name}_user') print(user_scripts_path)
但是可能是 pip 内部使用了不同的逻辑(可能基于
distutils ),但是结果应该仍然相同。参考文献