以编程方式确定pip的“用户安装”位置脚本目录

发布于 2021-01-29 15:03:39

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
关注者
0
被浏览
69
1 个回答
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看