如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/等)?就像是: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-baseC:\Users\myusername\AppData\Roaming\Python$ python -m site --user-base/home/myusername/.local
2 回答
慕斯王
TA貢獻1864條經驗 獲得超2個贊
我相信以下應該給出預期的結果
import os
import sysconfig
user_scripts_path = sysconfig.get_path('scripts', f'{os.name}_user')
print(user_scripts_path)
命令行:
python -c 'import os,sysconfig;print(sysconfig.get_path("scripts",f"{os.name}_user"))'
但可能是pip在內部使用了不同的邏輯(可能基于distutils),但結果應該還是一樣的。
守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
命令行:
python -c "import os, site; print(os.path.join(site.USER_BASE, 'Scripts' if os.name == 'nt' else 'bin'))"
功能:
import os, site
if os.name == 'nt':
bin_dir = 'Scripts'
else:
bin_dir = 'bin'
def get_user_install_bin_dir():
return os.path.join(site.USER_BASE, bin_dir)
添加回答
舉報
0/150
提交
取消
