windows11下安装使用python多环境管理工具--poetry
官网
基础环境
系统版本 | python版本 | poetry版本 |
---|---|---|
win11 23H2 | 多版本 + 3.6.8(默认) + 3.8.6 + 3.10.4 |
1.8.2 |
安装
https://python-poetry.org/docs/
我们选择脚本的方式安装
使用powershell安装
安装命令
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
注意:
这里需要特别提醒,如果默认的python环境是3.6的话,会报错,无法找到portry的包,就无法安装
因为我这里是多python版本,所以,遇到这个问题堵了我好久
解决方法就是到python3.10的安装目录下,然后右键打开powershell
然后再运行命令(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | .\python.exe -
就可以正常安装了
过程
Retrieving Poetry metadata
# Welcome to Poetry!
This will download and install the latest version of Poetry,
a dependency and package manager for Python.
It will add the `poetry` command to Poetry's bin directory, located at:
C:\Users\xxx\AppData\Roaming\Python\Scripts
You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.
Installing Poetry (1.8.2)
Installing Poetry (1.8.2): Creating environment
Installing Poetry (1.8.2): Installing Poetry
Installing Poetry (1.8.2): Creating script
Installing Poetry (1.8.2): Done
Poetry (1.8.2) is installed now. Great!
You can test that everything is set up by executing:
`poetry --version`
查看版本
poetry --version
Poetry (version 1.8.2)
这就表明安装成功
版本升级
poetry self update
升级预安装版本
poetry self update --preview
升级特定版本
poetry self update 1.2.0
1.1 版本的poetry无法原地更新到 1.2 或更新的版本。
要迁移到更新的版本,请使用您原来的安装方法卸载,然后使用上述方法重新安装。
卸载poetry
curl -sSL https://install.python-poetry.org | ./python - --uninstall
同样需要到安装poetry的python安装目录下运行
显示如下则表示卸载成功
Removing Poetry (1.8.2)
使用
https://python-poetry.org/docs/cli/
项目创建
poetry new my-package
会帮忙生成基础文件
my-package
├── pyproject.toml
├── README.md
├── my_package
│ └── __init__.py
└── tests
└── __init__.py
如果你想给你的项目命名一个与文件夹不同的名字,你可以传递 --name
选项:
poetry new my-folder --name my-package
这种方法我们用的少,因为其实很多时候我们只需要 pyproject.toml
文件
所以,这里就需要用到init方法了
项目初始化
手动创建项目目录
mkdir my-package
cd my-package
初始化项目
poetry init
This command will guide you through creating your pyproject.toml config.
Package name [my-package]:
Version [0.1.0]:
Description []:
Author [xxxx <xxxxx@163.com>, n to skip]:
License []:
Compatible Python versions [^3.10]:
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
[tool.poetry]
name = "my-package"
version = "0.1.0"
description = ""
authors = ["xxxx <xxxxx@163.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Do you confirm generation? (yes/no) [yes]
初始化安装
poetry install
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
Installing the current project: my-package (0.1.0)
Warning: The current project could not be installed: [Errno 2] No such file or directory: 'C:\\my-package\\README.md'
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.
In a future version of Poetry this warning will become an error!
上面的提示是当执行
poetry install
命令时,默认情况下Poetry会将当前目录下的项目视为一个可安装的Python包,并尝试构建和安装它。
在这个过程中,它通常会包括项目的根目录下的必要文件,如README.md
、LICENSE
等。
解决办法:
- 如果你并不希望安装当前项目本身,而只想安装该项目的依赖项,可以在命令后面加上
--no-root
参数:poetry install --no-root
- 如果你确实需要安装项目,但当前缺失了 README.md 文件,可以手动创建。
- 若你的意图只是使用Poetry进行依赖管理而不打包项目,可以在 pyproject.toml 文件中设置
package-mode = false
来禁用包模式。
不过请注意,这样做后Poetry将不再把当前项目当作一个可发布的包来对待,可能会影响到诸如构建、发布等操作。
全局选项
--verbose (-v|vv|vvv)
: "-v" 正常输出, "-vv" 详细输出 "-vvv" debug--help (-h)
: 帮助信息--quiet (-q)
: 不输出任何信息--ansi
: 强制 ANSI 输出--no-ansi
: 禁止ANSI 输出--version (-V)
: 显示版本--no-interaction (-n)
: 禁止交互询问
安装第三方库
poetry add requests==xxx
--group (-D)
: 分组--editable (-e)
: 添加到编辑模式--extras (-E)
: 添加额外的依赖--optional
: 添加至可选依赖--python
: 指定python版本--platform
: 指定操作系统--source
: 使用源名称安装--allow-prereleases
: 接受 prereleases 安装--dry-run
: 输出操作但不执行--lock
: 只更新锁定不安装--dev
:指定为开发依赖
移除第三方库
poetry remove xxx
--group (-D)
: 分组--dry-run
: 输出操作但不执行
列出所有可安装的包
poetry show
asgiref 3.7.2 ASGI specs, helper code, and adapters
beautifulsoup4 4.12.3 Screen-scraping library
bs4 0.0.2 Dummy package for Beautiful Soup (beautifulsoup4)
django 5.0.3 A high-level Python web framework that encourages rapid development and clean, pragmatic design.
mysqlclient 2.2.4 Python interface to MySQL
soupsieve 2.5 A modern CSS selector implementation for Beautiful Soup.
sqlparse 0.4.4 A non-validating SQL parser.
typing-extensions 4.10.0 Backported and Experimental Type Hints for Python 3.8+
tzdata 2024.1 Provider of IANA time zone data
--without
: 忽略依赖--with
: 同时显示--only
: 只显示指定的依赖--default
: 只显示默认的--no-dev
: 不显示开发的依赖--tree
: 以树状形式显示--latest (-l)
: 展示最新的版本--outdated (-o)
: 显示最新版本,但仅适用于过时的软件包
常用
--tree
poetry show --tree
bs4 0.0.2 Dummy package for Beautiful Soup (beautifulsoup4)
└── beautifulsoup4 *
└── soupsieve >1.2
django 5.0.3 A high-level Python web framework that encourages rapid development and clean, pragmatic design.
├── asgiref >=3.7.0,<4
│ └── typing-extensions >=4
├── sqlparse >=0.3.1
└── tzdata *
mysqlclient 2.2.4 Python interface to MySQL
读取 pyproject.toml
并安装相关依赖
# 需要进入pyproject.toml所在目录
poetry install
--without
: 忽略依赖--with
: 安装可选的依赖--only
: 只安装指定的依赖--default
: 只安装默认的依赖--sync
: 同步锁定的版本至环境中--no-root
: 不安装根依赖包--dry-run
: 输出操作但不执行--extras (-E)
: 安装额外的包
创建虚拟环境
poetry env use /full/path/to/python
在 poetry install
之前,可以切换任意版本的python,也就是用你所指定的python版本来创建虚拟环境
激活虚拟环境
# 需要进入pyproject.toml所在目录
poetry shell
Spawning shell within C:\Users\xxx\AppData\Local\pypoetry\Cache\virtualenvs\abc-yLlOvVWf-py3.10
PowerShell 7.4.1
(abc-py3.10) PS E:\xxxxx\xxx\xxxx>
会进入虚拟环境,就可以直接执行各种python命令
退出虚拟环境
exit
不需要进入虚拟环境的情况下,在虚拟环境中执行命令
只需要在命令前面添加 poetry run
poetry run xxx
查看虚拟环境信息
poetry env info
Virtualenv
Python: 3.10.4
Implementation: CPython
Path: C:\Users\xxx\AppData\Local\pypoetry\Cache\virtualenvs\abc-yLlOvVWf-py3.10
Executable: C:\Users\xxx\AppData\Local\pypoetry\Cache\virtualenvs\abc-yLlOvVWf-py3.10\Scripts\python.exe
Valid: True
Base
Platform: win32
OS: nt
Python: 3.10.4
Path: D:\python_env\Python310
Executable: D:\python_env\Python310\python.exe
会显示出虚拟环境的相关信息
查看虚拟环境列表
poetry env list
abc-yLlOvVWf-py3.10 (Activated)
删除虚拟环境
poetry env remove abc-yLlOvVWf-py3.10
搜索远程包
poetry search xxx
锁定版本
poetry lock
对环境进行更新
poetry update
导出锁定的文件为其他的格式
poetry export -f requirements.txt --output requirements.txt
--format (-f)
: 转换的格式,暂时只支持requirements.txt--output (-o)
: 输出文件名字--dev
: 包括开发的依赖--extras (-E)
: 额外的依赖--without-hashes
: 忽略哈希--with-credentials
: 包括合格证书
修改仓库源
[[tool.poetry.source]]
name = "douban"
url = "https://pypi.doubanio.com/simple/"
本文来自博客园,作者:厚礼蝎,转载请注明原文链接:https://www.cnblogs.com/guangdelw/p/18053758
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律