当前位置: 首页 > news >正文

poetry安装

poetry安装方式

安装地址:https://python-poetry.org/docs/#installing-with-the-official-installer

根据官方推荐安装方式(with official installer)的安装步骤:

  1. 打开powershell运行(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -(如果本地已经安装了Python或安装了Anaconda,则将py替换为python)

2、继续在poershell中添加环境变量:

setx PATH "%APPDATA%\Python\Scripts;%PATH%"

3、重启powershell,并运行poetry --version查看安装情况,若出现版本号则安装成功
在这里插入图片描述

在已有项目中使用poetry管理Python环境

step1:项目初始化
如果项目还没有使用 Poetry,需要先初始化:

poetry init

该命令会创建一个 pyproject.toml 文件,你可以按照提示填写项目信息,或者直接按回车使用默认值。

step2:安装依赖
如果项目已经有 pyproject.toml 文件:

poetry install

该命令会:

  • 创建一个虚拟环境(如果还没有)
  • 安装 pyproject.toml 中列出的所有依赖
  • 生成/更新 poetry.lock 文件确保依赖版本一致

项目结构报错及解决方案

报错为:

Error: The current project could not be installed: No file/folder
found for package ai-agent-test

分析项目结构为:

C:.
│  poetry.lock
│  pyproject.toml
│  README.md
└─src
        demo.py
        from agents import Agent, Runner.py
        test.py
        test2.py
        __init__.py

pyprojects.toml文件内容为:

[project]
name = "ai-agent-test"
version = "0.1.0"
description = ""
authors = [
    {name = "pengkangzhen",email = "kangzhenpeng@outlook.com"}
]
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
    "seaborn",
    "numpy",
]


[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"

执行poetry install报错如下:

PS C:\Users\pengkangzhen\PythonProjects\ai-agent-test> poetry install
Updating dependencies
Resolving dependencies... (7.4s)

Package operations: 15 installs, 0 updates, 0 removals

  - Installing numpy (2.2.4)
  - Installing six (1.17.0)
  - Installing contourpy (1.3.1)
  - Installing cycler (0.12.1)
  - Installing fonttools (4.57.0)
  - Installing kiwisolver (1.4.8)
  - Installing packaging (24.2)
  - Installing pillow (11.1.0)
  - Installing pyparsing (3.2.3)
  - Installing python-dateutil (2.9.0.post0)
  - Installing pytz (2025.2)
  - Installing tzdata (2025.2)
  - Installing matplotlib (3.10.1)
  - Installing pandas (2.2.3)
  - Installing seaborn (0.13.2)

Writing lock file

Installing the current project: ai-agent-test (0.1.0)
Error: The current project could not be installed: No file/folder found for package ai-agent-test
If you do not want to install the current project use --no-root.
If you want to use Poetry only for dependency management but not for packaging, you can disable package mode by setting package-mode = false in your pyproject.toml file.
If you did intend to install the current project, you may need to set `packages` in your pyproject.toml file.

错误原因

  1. 缺少 packages 配置。当前 pyproject.toml 中未定义 packages 字段,导致 Poetry 无法识别项目的代码位置。默认情况下,Poetry 会尝试在根目录下查找包(如 ai-agent-test/ 目录),但你的代码实际位于 src/ 目录中,因此匹配失败。
  2. 项目结构问题。项目代码放在 src/ 目录下,但 Poetry 默认不会自动扫描 src/ 目录,除非显式配置。

解决方案

方法 1:修改 pyproject.toml(推荐)
在 pyproject.toml 中添加 packages 配置,明确指定代码路径:

[tool.poetry]
name = "ai-agent-test"
# ...其他配置...

# 添加以下内容
packages = [
    { include = "ai-agent-test", from = "src" }
]

方法 2:调整项目结构
将代码从 src/ 移动到根目录下的 ai-agent-test/ 目录(与项目名一致):

├── pyproject.toml
├── ai-agent-test/  # 代码移到这里
│   ├── __init__.py
│   ├── demo.py
│   └── ...
└── src/  # 删除或保留(不再使用)

peotry项目结构

在使用 Poetry 和 VSCode 管理 Python 项目时,推荐的标准项目结构如下:


项目结构示例

a_demo_test/
├── src/                    # 主源码目录(推荐)
│   └── your_package_name/  # 项目核心包(与项目名相同,需在 pyproject.toml 中声明)
│       ├── __init__.py     # 表明这是 Python 包
│       └── main.py         # 逻辑代码入口
├── tests/                  # 单元测试目录
│   └── test_sample.py      # 测试代码(格式:test_*.py)
├── pyproject.toml          # Poetry 项目配置和依赖清单
├── README.md               # 项目文档
└── .gitignore              # Git 忽略规则

a_demo_test/
├── src/                   # 主源码目录
├── tests/                 # 测试代码
├── data/                  # 数据文件目录(可自定义名称)
│   ├── input/             # 输入数据(如配置文件、初始数据集)
│   └── output/            # 程序生成的持久化结果(如 CSV、JSON 文件)
├── logs/                  # 日志文件
├── temp/                  # 临时文件(程序运行中生成,可定期清理)
├── pyproject.toml         # Poetry 配置
└── .gitignore             # 需忽略非源码文件

核心结构说明

1. 源码位置
  • 推荐位置:放在 src/your_package_name 中(替换 your_package_name 为实际包名)。
  • 优势
    • 避免开发者误导入本地文件(如 tests 中的代码意外依赖工作目录的配置)。
    • 符合 Python 官方打包规范 (PEP 621)。
2. 测试代码
  • 位置:tests/ 目录。
  • 工具:推荐使用 pytest(在 pyproject.toml 中添加依赖)。
3. 依赖管理
  • 初始化项目:在项目根目录运行:
    poetry init  # 交互式生成 pyproject.toml
    poetry install  # 安装依赖并创建虚拟环境
    
  • 安装新依赖:
    poetry add requests  # 添加并写入 pyproject.toml
    
4. VSCode 配置建议
  1. 选择虚拟环境解释器
    • 按下 Ctrl+Shift+P → 输入 Python: Select Interpreter → 选择 Poetry 创建的虚拟环境。
  2. 测试集成
    .vscode/settings.json 中添加:
    {
      "python.testing.pytestArgs": ["tests"],
      "python.testing.unittestEnabled": false,
      "python.testing.pytestEnabled": true
    }
    

示例pyproject.toml片段

[tool.poetry]
name = "your_package_name"  # 必须与 src/ 下的包名一致
version = "0.1.0"
packages = [{ include = "your_package_name", from = "src" }]  # 关键配置!

[tool.poetry.dependencies]
python = "^3.9"
requests = "*"

[tool.poetry.group.test.dependencies]
pytest = "^7.0"

其他注意事项

  • 无需 __init__.py 的目录
    tests/src/ 本身不需要此文件,但包目录(如 src/your_package_name)必须有。
  • 模块导入正确性
    在代码中使用绝对导入(如 from your_package_name import utils)。

通过遵循此结构,你的项目将易于维护、测试和分发。


文章转载自:
http://cephalothin.wanhuigw.com
http://allier.wanhuigw.com
http://avellan.wanhuigw.com
http://cholla.wanhuigw.com
http://aroid.wanhuigw.com
http://beluchistan.wanhuigw.com
http://bisync.wanhuigw.com
http://bannerline.wanhuigw.com
http://bedight.wanhuigw.com
http://apollonian.wanhuigw.com
http://antivirus.wanhuigw.com
http://alanyl.wanhuigw.com
http://busybody.wanhuigw.com
http://casablanca.wanhuigw.com
http://bingy.wanhuigw.com
http://ammocolous.wanhuigw.com
http://aslope.wanhuigw.com
http://bureaux.wanhuigw.com
http://bordel.wanhuigw.com
http://chinghai.wanhuigw.com
http://catatonia.wanhuigw.com
http://carry.wanhuigw.com
http://calendry.wanhuigw.com
http://bobbed.wanhuigw.com
http://bungle.wanhuigw.com
http://cephalometric.wanhuigw.com
http://cheralite.wanhuigw.com
http://bating.wanhuigw.com
http://antinatalism.wanhuigw.com
http://celestialize.wanhuigw.com
http://www.dtcms.com/a/111424.html

相关文章:

  • Transformer+BO-SVM时间序列预测(Matlab)
  • 第十五届蓝桥杯大赛软件赛省赛Python 大学 C 组:5.回文数组
  • 系统分析师-前6章总结
  • STM32单片机入门学习——第14节: [6-2] 定时器定时中断定时器外部时钟
  • PGSQL 对象创建函数生成工具
  • RSA和ECC在密钥长度相同的情况下哪个更安全?
  • 深度学习中的 Batch 机制:从理论到实践的全方位解析
  • AcWing 6118. 蛋糕游戏
  • Ubuntu安装Podman教程
  • Spring 核心技术解析【纯干货版】- XXI:Spring 第三方工具整合模块 Spring-Context-Suppor 模块精讲
  • 《古龙群侠传》游戏秘籍
  • 【 <二> 丹方改良:Spring 时代的 JavaWeb】之 Spring Boot 中的监控:使用 Actuator 实现健康检查
  • 【spring cloud Netflix】Eureka注册中心
  • 关于uint8_t、uint16_t、uint32_t、uint64_t的区别与分析
  • Linux(2025.3.15)
  • 安装 TabbyAPI+Exllamav2 和 vLLM 的详细步骤
  • 前后端通信指南
  • C# Winform 入门(7)之简单的抽奖系统邮件
  • #管理Node.js的多个版本
  • 虚拟现实 UI 设计:打造沉浸式用户体验
  • MINIQMT学习课程Day10
  • 欧几里得算法求最大公约数、最小公倍数
  • chromium魔改——CDP(Chrome DevTools Protocol)检测01
  • CCF GESP C++编程 八级认证真题 2025年3月
  • MySQL 性能调优:数据库的极限运动训练
  • [ deepseek 指令篇章 ]300个领域和赛道喂饭级deepseek指令
  • 【数论】 质数
  • 【已解决】Webstorm 每次使用 git pull/push 都要输入令牌/密码登录
  • RFC6937 PRR 的兑换细节
  • [2017][note]基于空间交叉相位调制的两个连续波在few layer铋Bi中的全光switch——