poetry安装方式
安装地址:https://python-poetry.org/docs/#installing-with-the-official-installer
根据官方推荐安装方式(with official installer)的安装步骤:
- 打开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.
错误原因
- 缺少 packages 配置。当前 pyproject.toml 中未定义 packages 字段,导致 Poetry 无法识别项目的代码位置。默认情况下,Poetry 会尝试在根目录下查找包(如 ai-agent-test/ 目录),但你的代码实际位于 src/ 目录中,因此匹配失败。
- 项目结构问题。项目代码放在 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 配置建议
- 选择虚拟环境解释器:
- 按下
Ctrl+Shift+P
→ 输入Python: Select Interpreter
→ 选择 Poetry 创建的虚拟环境。
- 按下
- 测试集成:
在.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
)。
通过遵循此结构,你的项目将易于维护、测试和分发。