**更新于 2024-05-17 **
最新的 poetry 的安装方式如下,需要注意的是下面windows的命令需要使用powershell执行:

# Linux/macOS
export POETRY_INSTALLER_REPO=https://mirrors.tuna.tsinghua.edu.cn/python-poetry/

curl -sSL https://install.python-poetry.org | python -

# Windows(PowerShell)
$env:POETRY_INSTALLER_REPO = "https://mirrors.tuna.tsinghua.edu.cn/python-poetry/"

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -

执行以上命令安装完成后,还需要将其可执行文件目录添加到系统环境变量 PATH 中才能全局使用。以下是具体解决方案和验证步骤:

解决方案(推荐选择A)

方法A:永久添加到用户级PATH(推荐)

在PowerShell中执行以下命令,将Poetry的bin目录永久添加到当前用户的PATH环境变量:

[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path", "User") + ";C:\Users\xingmu\AppData\Roaming\Python\Scripts", "User")

优点

  • 修改后对所有新终端会话生效。
  • 无需每次手动配置。
方法B:仅在PowerShell会话中临时生效

若使用PowerShell 6+,可通过以下脚本将路径追加到$PROFILE,每次启动PowerShell时自动检查并添加PATH:

echo 'if (-not (Get-Command poetry -ErrorAction Ignore)) { $env:Path += ";C:\Users\xingmu\AppData\Roaming\Python\Scripts" }' | Out-File -Append $PROFILE

适用场景

  • 仅需在当前用户PowerShell中使用Poetry。
  • 避免全局修改PATH。
替代方案:显式调用

直接通过完整路径调用Poetry(无需修改PATH):

C:\Users\xingmu\AppData\Roaming\Python\Scripts\poetry --version

验证安装

执行以下命令检查配置是否成功:

poetry --version

预期输出:

Poetry (version 2.1.3)

常见问题排查

  1. 命令仍不可用

    • 确保已重启终端或执行refreshenv刷新环境变量。
    • 检查路径拼写是否正确(如xingmu是否为您的用户名)。
  2. 权限问题

    • 若使用管理员权限安装Poetry,需确保PATH修改作用于同一用户。
  3. 路径冲突

    • 若存在多个Python版本,确认C:\Users\xingmu\AppData\Roaming\Python\Scripts是Poetry的实际安装路径。

补充说明

  • 镜像加速(国内用户):若后续安装依赖缓慢,可配置清华镜像源:
    poetry config repositories.pypi https://pypi.tuna.tsinghua.edu.cn/simple
    
  • 卸载Poetry:如需重装,运行:
    (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python - --uninstall
    

通过以上步骤即可完成配置。如有其他问题,可参考Poetry官方文档


如下方法更新于 2021-07-04 已过时

目前最新版的poetry1.2.0a1,安装方式有所改变,安装脚本变成了install-poetry.py

  1. https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py 这个python 脚本存储到本地,比如存储为 install-poetry.py
  2. 运行 python install-poetry.py --version 1.2.0a1
  3. 配置环境变量,就可以愉快的使用了。

“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”

Poetry官方文件给出了mac/linux/windows的在线安装说明,如下图
在这里插入图片描述
这个操作在 linux 上没有任何进度条提示,要等很久,很容易就放弃了,windows 上的这个命令我无法执行成功,这里给大家介绍一种离线安装的方法。

  1. https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py 这个python 脚本存储到本地,比如存储为 get-poetry.py
  2. https://github.com/python-poetry/poetry/releases 下载对应平台的 realse 版本,比如 1.1.4windows 版本 poetry-1.1.4-win32.tar.gz
  3. 运行 python get-poetry.py --file poetry-1.1.4-win32.tar.gz
  4. 配置环境变量,就可以愉快的使用了。

配置文件

poetry 的配置文件分别位于下面的位置:

  • macOS: ~/Library/Application Support/pypoetry
  • Windows: C:\Users<username>\AppData\Roaming\pypoetry
# 获取配置信息
$ poetry config --list

	cache-dir = "C:\\Users\\xingmu\\AppData\\Local\\pypoetry\\Cache"
	experimental.new-installer = true
	virtualenvs.create = true
	virtualenvs.in-project = null
	virtualenvs.path = "{cache-dir}\\virtualenvs"  # C:\Users\xingmu\AppData\Local\pypoetry\Cache\virtualenvs
# 修改缓存目录地址
$ poetry config cache-dir "f:\\.cache\poetry"

配置项目专用的源

windows 中 poetry 会自动去找 pip 的全局配置,在 linux 下我发现并没有去找$HOME/.pip/pip.conf的配置,具体原因不太清楚,对应此种情况,可以在项目跟路径下的pyproject.toml中添加如下配置,来指定项目的安装源。

注意是双中括号。

[[tool.poetry.source]]
name = "aliyun"
url = "https://mirrors.aliyun.com/pypi/simple/"
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐