Poetry 在 Linux 和 Windows 系统中的安装步骤

Poetry 在 Linux 和 Windows 系统中的安装步骤区分默认安装和指定目录安装 ,并包括配置环境变量(如 POETRY_HOME、镜像源等)的说明。


✅ Poetry 安装指南

📌 一、Linux 系统(如 Ubuntu)


🔹 默认安装步骤

1. 安装 Poetry(默认路径)
bash 复制代码
curl -sSL https://install.python-poetry.org | python3 -
  • 默认安装路径为:~/.local/share/pypoetry/venv
  • 可执行文件路径为:~/.local/bin/poetry
2. 将 poetry 添加到 PATH
bash 复制代码
export PATH="$HOME/.local/bin:$PATH"
3. 验证安装
bash 复制代码
poetry --version
4. 永久生效(直接执行以下命令,写入 shell 配置)
bash 复制代码
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

🔹 指定目录安装步骤

1. 设置自定义安装目录(如 /opt/poetry
bash 复制代码
export POETRY_HOME="/opt/poetry"

注意:你需要有写入 /opt/poetry 的权限,可以使用 sudo 或修改目录权限。

2. 安装 Poetry 到指定目录
bash 复制代码
curl -sSL https://install.python-poetry.org | python3 -
  • 安装路径变为:/opt/poetry/venv
  • 可执行文件路径为:/opt/poetry/bin/poetry
3. 将 poetry 添加到 PATH
bash 复制代码
export PATH="$POETRY_HOME/bin:$PATH"
4. 设置镜像源(可选)
bash 复制代码
export POETRY_PYPI_MIRROR="https://pypi.tuna.tsinghua.edu.cn/simple"
5. 永久生效(直接执行以下命令,写入 shell 配置)
bash 复制代码
echo 'export POETRY_HOME="/opt/poetry"' >> ~/.bashrc
echo 'export PATH="$POETRY_HOME/bin:$PATH"' >> ~/.bashrc
echo 'export POETRY_PYPI_MIRROR="https://pypi.tuna.tsinghua.edu.cn/simple"' >> ~/.bashrc
source ~/.bashrc

📌 二、Windows 系统(手动配置环境变量)


🔹 默认安装步骤

1. 使用 PowerShell 安装 Poetry(默认路径)
powershell 复制代码
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
  • 默认安装路径为:%APPDATA%\Python\Scripts\poetry.exe
2. 手动将 poetry 添加到系统环境变量 PATH
步骤如下:
  1. 右键点击"此电脑" → 属性 → 高级系统设置 → 环境变量。

  2. 在"用户变量"或"系统变量"中找到 Path,点击"编辑"。

  3. 点击"新建",输入以下路径(根据你的 Python 安装位置调整):

    C:\Users<用户名>\AppData\Roaming\Python\Scripts

  4. 确认保存所有更改。

3. 验证安装是否成功

重启一个终端窗口(CMD 或 PowerShell),运行:

cmd 复制代码
poetry --version

🔹 指定目录安装步骤

1. 手动设置 POETRY_HOME 环境变量
步骤如下:
  1. 右键点击"此电脑" → 属性 → 高级系统设置 → 环境变量。
  2. 在"用户变量"或"系统变量"区域中:
    • 点击"新建"
    • 变量名输入:POETRY_HOME
    • 变量值输入:D:\tools\poetry(或其他你希望的路径)
2. 安装 Poetry 到指定目录

在 PowerShell 中运行:

powershell 复制代码
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
  • Poetry 会自动识别你设置的 POETRY_HOME 并安装到该目录下的 bin 文件夹中:

    复制代码
    D:\tools\poetry\bin\poetry.exe
3. 将 poetry 添加到系统环境变量 PATH
  1. 回到"环境变量"窗口。

  2. 编辑 Path,添加新项:

    D:\tools\poetry\bin

4. 手动设置 PyPI 镜像源(可选)
  1. 回到"环境变量"窗口。
  2. 新建一个变量:
    • 变量名:POETRY_PYPI_MIRROR
    • 变量值:https://pypi.tuna.tsinghua.edu.cn/simple
5. 验证安装是否成功

重启一个终端窗口,运行:

cmd 复制代码
poetry --version

✅ 总结对比表

功能 Linux 默认安装 Linux 自定义目录安装 Windows 默认安装 Windows 自定义目录安装
安装命令 `curl ... python3 -` `curl ... python3 -`
默认安装路径 ~/.local/share/pypoetry/venv /opt/poetry/venv %APPDATA%\Python\Scripts\poetry.exe %POETRY_HOME%\bin\poetry.exe
添加到 PATH export PATH=... export PATH=... 手动添加 Python\Scripts 到 Path 手动添加 %POETRY_HOME%\bin 到 Path
设置镜像源 export POETRY_PYPI_MIRROR=... export POETRY_PYPI_MIRROR=... 手动设置系统变量 POETRY_PYPI_MIRROR 手动设置系统变量 POETRY_PYPI_MIRROR
永久生效 写入 .bashrc / .zshrc 写入 .bashrc / .zshrc 修改系统环境变量 修改系统环境变量

相关推荐
奔跑吧 android40 分钟前
【linux kernel 常用数据结构和设计模式】【数据结构 2】【通过一个案例属性list、hlist、rbtree、xarray数据结构使用】
linux·数据结构·list·kernel·rbtree·hlist·xarray
yzx99101344 分钟前
生活在数字世界:一份人人都能看懂的网络安全生存指南
运维·开发语言·网络·人工智能·自动化
橙*^O^*安2 小时前
Go 语言基础:变量与常量
运维·开发语言·后端·golang·kubernetes
默默无名的大学生2 小时前
数据结构—顺序表
数据结构·windows
NiKo_W2 小时前
Linux 文件系统与基础指令
linux·开发语言·指令
湖南馒头2 小时前
【Win11 启动项BCD文件修复教程】
windows·系统恢复·win1
阿拉斯加大闸蟹4 小时前
基于RDMA 通信的可负载均衡高性能服务架构
运维·架构·负载均衡
Darkwanderor4 小时前
Linux 的权限详解
linux
SabreWulf20204 小时前
Ubuntu 20.04手动安装.NET 8 SDK
linux·ubuntu·avalonia·.net8
不是吧这都有重名4 小时前
为什么ubuntu大文件拷贝会先快后慢?
linux·运维·ubuntu