侧边栏壁纸
  • 累计撰写 103 篇文章
  • 累计创建 46 个标签
  • 累计收到 5 条评论

目 录CONTENT

文章目录

Python 使用 pipx 安装第三方库包并配置安装目录

勤为径苦作舟
2023-11-27 / 0 评论 / 0 点赞 / 132 阅读 / 0 字

简介

pipx 可以给安装的每个软件应用都创建一个虚拟环境,然后在全局 /bin 中创建二进制文件链接,之后升降 Python 都不会影响。

源码:GitHub - pypa/pipx: Install and Run Python Applications in Isolated Environments
官网:https://pypa.github.io/pipx/

环境说明

我在 Windows 下的习惯是安装 Python 后,设置如下环境变量:
PYTHON_PATHD:\ProgramFiles\Python\Python310(Python 安装目录)
Path:追加%PYTHON_PATH%;%PYTHON_PATH%\Scripts;

安装 pipx 并用其安装软件包

pipx 默认的二进制文件链接路径为~/.local/bin,可配置环境变量PIPX_BIN_DIR修改。
默认的虚拟环境路径为~/.local/pipx,可配置环境变量PIPX_HOME修改。

PIPX_HOMED:\Cache\pipx
PIPX_BIN_DIR%PIPX_HOME%\bin
Path:新增%PIPX_BIN_DIR%;,注意配置在 Python 安装目录的 Scripts 目录前,否则先找这个目录可能会报错。

Linux 中可以先不配置。

# 使用 pip 安装 pipx
$ pip install pipx

# pipx 查看环境变量
$ pipx environment
PIPX_HOME=D:\Cache\pipx
PIPX_BIN_DIR=D:\Cache\pipx\bin
PIPX_SHARED_LIBS=D:\Cache\pipx\shared
PIPX_LOCAL_VENVS=D:\Cache\pipx\venvs
PIPX_LOG_DIR=D:\Cache\pipx\logs
PIPX_TRASH_DIR=D:\Cache\pipx\.trash
PIPX_VENV_CACHEDIR=D:\Cache\pipx\.cache

Only PIPX_HOME and PIPX_BIN_DIR can be set by users in the above list.

# 换源
$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# pipx 安装 poetry 测试
$ pipx install poetry
  installed package poetry 2.1.3, installed using Python 3.10.18
  These apps are now globally available
    - poetry
⚠  Note: '/home/duanluan/.local/bin' is not on your PATH environment variable. These apps will not be globally accessible
    until your PATH is updated. Run `pipx ensurepath` to automatically add it, or manually modify your PATH in your shell's
    config file (e.g. ~/.bashrc).
done! ✨ 🌟 ✨

# 没有手动设置环境变量时运行
$ pipx ensurepath
Success! Added /home/duanluan/.local/bin to the PATH environment variable.

Consider adding shell completions for pipx. Run 'pipx completions' for instructions.

You will need to open a new terminal or re-login for the PATH changes to take effect. Alternatively, you can source your
shell's config file with e.g. 'source ~/.bashrc'.

Otherwise pipx is ready to go! ✨ 🌟 ✨

$ source ~/.bashrc
# 运行 poetry
$ poetry -V
Poetry (version 2.1.3)

Windows 如果运行 poetry 报找不到,则将追加到系统环境变量Path%PIPX_BIN_DIR%;也同时追加到用户环境变量试下。或者注销系统用户再试下。

0

评论区