背景简介
使用 Poetry 管理已有Python项目。
前置信息
- Poetry 已安装【官方参考】
详细信息
流程图
实现步骤
第一步: 执行初始化,根据自身需求填写即可。
poetry init
This command will guide you through creating your pyproject.toml config.
Package name [pers-website-backend]:
Version [0.1.0]:
Description []: personal website backend
Author [Henry Lin <huarui.lin@outlook.com>, n to skip]:
License []:
Compatible Python versions [>=3.13]:
Would you like to define your main dependencies interactively? (yes/no) [yes]
You can specify a package in the following forms:
- A single name (requests): this will search for matches on PyPI
- A name and a constraint (requests@^2.23.0)
- A git url (git+https://github.com/python-poetry/poetry.git)
- A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)
- A file path (../my-package/my-package.whl)
- A directory (../my-package/)
- A url (https://example.com/packages/my-package-0.1.0.tar.gz)
Package to add or search for (leave blank to skip):
Would you like to define your development dependencies interactively? (yes/no) [yes]
Package to add or search for (leave blank to skip):
Generated file
[project]
name = "pers-website-backend"
version = "0.1.0"
description = "personal website backend"
authors = [
{name = "Henry Lin",email = "huarui.lin@outlook.com"}
]
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
Do you confirm generation? (yes/no) [yes]
- 查看初始化的 pyproject.toml 文件
[project]
name = "pers-website-backend"
version = "0.1.0"
description = "personal website backend"
authors = [
{name = "Henry Lin",email = "huarui.lin@outlook.com"}
]
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
第二步: 导入已有依赖
- 创建并激活虚拟环境
poerty install
Installing dependencies from lock file
报错参考:【Poetry - Issue - The current project could not be installed: No file/folder found for package XXX 】
- 添加原有依赖
poetry run pip install -r requirements.txt
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting fastapi==0.115.0 (from -r requirements.txt (line 7))
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/06/ab/a1f7eed031aeb1c406a6e9d45ca04bff401c8a25a30dd0e4fd2caae767c3/fastapi-0.115.0-py3-none-any.whl (94 kB)
Collecting uvicorn==0.30.6 (from uvicorn[standard]==0.30.6->-r requirements.txt (line 8))
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/f5/8e/cdc7d6263db313030e4c257dd5ba3909ebc4e4fb53ad62d5f09b1a2f5458/uvicorn-0.30.6-py3-none-any.whl (62 kB)
Collecting SQLAlchemy==2.0.35 (from -r requirements.txt (line 11))
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/0e/c6/33c706449cdd92b1b6d756b247761e27d32230fd6b2de5f44c4c3e5632b2/SQLAlchemy-2.0.35-py3-none-any.whl (1.9 MB)
Requirement already satisfied: asyncpg==0.31.0 in /Users/huaruilin/Library/Caches/pypoetry/virtualenvs/pers-website-backend-OOAazPPH-py3.13/lib/python3.13/site-packages (from -r requirements.txt (line 12)) (0.31.0)
******
charset_normalizer-3.4.4 click-8.3.1 click-didyoumean-0.3.1 click-plugins-1.1.1.2 click-repl-0.3.0 cryptography-43.0.3 decorator-5.2.1 deprecated-1.3.1 dnspython-2.8.0 email-validator-2.3.0 et-xmlfile-2.0.0 fastapi-0.115.0 flower-2.0.1 frozenlist-1.8.0 greenlet-3.2.4 h11-0.16.0 html5lib-1.1 httpcore-1.0.9 httptools-0.7.1 httpx-0.27.2 humanize-4.15.0 idna-3.11 iniconfig-2.3.0 jsonpath-0.82.2 kombu-5.6.1 limits-5.6.0 lxml-6.0.2 mini-racer-0.13.2 multidict-6.7.0 nest_asyncio-1.6.0 numpy-2.4.0 openpyxl-3.1.5 packaging-25.0 pandas-2.3.3 pluggy-1.6.0 prometheus-client-0.23.1 prompt-toolkit-3.0.52 propcache-0.4.1 psycopg-3.3.2 psycopg-binary-3.3.2 pycparser-2.23 pydantic-2.9.2 pydantic-core-2.23.4 pydantic-settings-2.5.2 pytest-8.3.3 pytest-asyncio-0.24.0 pytest-mock-3.14.0 python-dateutil-2.9.0.post0 python-dotenv-1.0.1 pytz-2025.2 pyyaml-6.0.3 redis-5.2.0 requests-2.32.5 six-1.17.0 slowapi-0.1.9 sniffio-1.3.1 soupsieve-2.8.1 starlette-0.38.6 tabulate-0.9.0 tornado-6.5.4 tqdm-4.67.1 typing-extensions-4.15.0 tzdata-2025.3 urllib3-2.6.2 uvicorn-0.30.6 uvloop-0.22.1 vine-5.1.0 watchfiles-1.1.1 wcwidth-0.2.14 webencodings-0.5.1 websockets-15.0.1 wrapt-2.0.1 xlrd-2.0.2 yarl-1.22.0 zhipuai-2.1.4
Note: 只是安装了对应库,并没有写入 pyproject.toml 文件
以上便是本文的全部内容,感谢您的阅读,如遇到任何问题,欢迎在评论区留言讨论。