Python 依赖管理及打包三方库 Poetry

一、背景

最近python的ui自动化工程中需要调研和选取一个python第三方包的依赖管理工具,近期poetry比较火,就选择了这个新的工具,以下来介绍Python环境管理Poetry的使用。

介绍:

Poetry 是 Python 中依赖管理和打包的工具。他可以管理项目中的第三包的依赖(安装/更新)。同时也提供了一个锁定文件以确保可重复安装,并且可以构建项目以供分发。 poetry的Python版本要求为 3.7+,且是多平台的。

二、安装和使用

1 安装 (在windows平台下)

Poetry官网:https://python-poetry.org/docs/

1.1 安装Poetry

Windows (Powershell)

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

根据官网安装的提示:如果是在Microsoft Store上安装的python,需要将上面的py替换为python,但是我直接安装,提示 py : 无法将“py”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。必须要把py替换为python。

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

使用 poetry –version 检查版本, 如果提示版本号则安装成功。

1.2 配置poetry poetry

安装后默认的缓存路径个虚拟环境路径在:

C:\Users\<your name>\AppData\Local\pypoetry\Cache 为了不占用C盘空间,修改默认的虚拟环境的目录。

# 此处直接修改 cache-dir 就会修改 cache-dir 和 virtualenvs.path 的路径 poetry config cache-dir D:\\poetry_enev1.2. 通过 poetry config –list 查看已经修改成功 取消之前的修改: poetry config cache-dir –unset

mac安装 brew intall poetry

2 使用poetry创建一个新项目

2.1 直接使用poetry 创建项目

poetry new project_name 自动创建一个project_name的文件件,项目名称也叫做project_name。 2.2 添加python依赖 # 在pyproject.toml 同目录下 poetry add numpy=1.2 安装的包和版本都会在 pyproject.toml 中

[tool.poetry.dependencies] python = “^3.9” numpy = “^1.24.2”

2.3追踪&更新包 输入 poetry show -h 可以查看poetry show之后可以加什么参数:

编辑切换为居中

添加图片注释,不超过 140 字(可选)

poetry show :查看项目安装的依赖

编辑切换为居中

添加图片注释,不超过 140 字(可选)

poetry show -t :树形结构查看项目安装的依赖

编辑切换为居中

添加图片注释,不超过 140 字(可选)

这里解释下colorama >= 0.4.1,<0.5.0。 安装某个包时,会在pyproject.toml文件中默认使用版本限定,比如colorama = “^0.4.1” ,当我执行 poetry update 时,colorama也许会更新到0.4.9,但绝不会更新到0.5.0,意思是在更新依赖时不会修改最左边非零的数字号版本,这样的默认设定可以确保不会更新到不兼容变动的版本。 poetry update:更新所有锁定版本的依赖 poetry update httprunner :更新指定的依赖 2.4卸载依赖

编辑

添加图片注释,不超过 140 字(可选)

2.5 运行项目或者文件

poetry run python run.py

备注:本地调试main函数也是用这个方法,run.py替换成你想调试的py文件

3 在现有项目中使用poetry

3.1 在某个目录下 使用poetry init

poetry init 使用这种交互式的方式创建项目,根据选项设置完成之后,在对应的目录下同样会生成 pyproject.toml 文件。

4 拿到一个使用poetry管理依赖的新项目

4.1 在pyproject.toml同目录下 poetry install Tips: 使用poetry时会生成 poetry.lock,此文件推荐上传到git版本控制中

5 手动管理依赖

5.1 在 pyproject.toml文件同目录下,进入虚拟环境 poetry shell 使用此命令,进入当前的项目虚拟环境,接下来就像平常使用python一样了。 5.2 退出 deactivate # 或者直接退出 这个是shell

6 管理环境

6.1 获取当前的虚拟环境的基本信息 poetry env info

6.2 切换虚拟环境 poetry env use 具体的环境的 python 可执行文件路径

6.3 删除环境 poetry env remove

7 在Pycharm中配置poetry环境

一般拿到一个含有poetry环境的项目,使用pycharm打开,会自动识别poetry解释器。如果没有识别 需要手动添加,如下所示: 基础解释器:选择对应虚拟环境的Python可执行文件。 Poetry可执行文件,选择安装在本地的Poetry可执行文件。

编辑切换为居中

添加图片注释,不超过 140 字(可选)

配置完毕,打开pycharm底部导航栏控制台,即可显示当前虚拟环境前缀,接下来就可以操作就按照正常的流程执行了。

8.打包发布

8.1 打包 poetry build 8.2 pypi发布 publish

9 总结

一般情况下我们使用poetry管理项目,无非就是两种情况:新建项目,可以使用poetry new 项目名称, 或者是在旧的项目上使用poetry,那么就是用poetry init 使用交互式初始化一个项目。 平常安装依赖或者是更新依赖 就使用 poetry add/update 依赖名。

10.扩展

更多命令行

(onemobile-apicase-alYbnEg_-py3.10) guowenhe@guowenhe-2 onemobile-apicase % poetry –help Poetry version 1.1.13 ​ USAGE poetry [-h] [-q] [-v [<…>]] [-V] [–ansi] [–no-ansi] [-n] <command> [<arg1>] … [<argN>] ​ ARGUMENTS <command> The command to execute <arg> The arguments of the command ​ GLOBAL OPTIONS -h (–help) Display this help message -q (–quiet) Do not output any message -v (–verbose) Increase the verbosity of messages: “-v” for normal output, “-vv” for more verbose output and “-vvv” for debug -V (–version) Display this application version –ansi Force ANSI output –no-ansi Disable ANSI output -n (–no-interaction) Do not ask any interactive question ​ AVAILABLE COMMANDS about Shows information about Poetry. add Adds a new dependency to pyproject.toml. build Builds a package, as a tarball and a wheel by default. cache Interact with Poetry’s cache check Checks the validity of the pyproject.toml file. config Manages configuration settings. debug Debug various elements of Poetry. env Interact with Poetry’s project environments. export Exports the lock file to alternative formats. help Display the manual of a command init Creates a basic pyproject.toml file in the current directory. install Installs the project dependencies. lock Locks the project dependencies. new Creates a new Python project at <path>. publish Publishes a package to a remote repository. remove Removes a package from the project dependencies. run Runs a command in the appropriate environment. search Searches for packages on remote repositories. self Interact with Poetry directly. shell Spawns a shell within the virtual environment. show Shows information about packages. update Update the dependencies as according to the pyproject.toml file. version Shows the version of the project or bumps it when a valid bump rule is provided.

​ ​ 官网入口 poetry (https://python-poetry.org/docs/#installing-with-the-official-installer)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年12月11日
下一篇 2023年12月11日

相关推荐