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之后可以加什么参数:

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

 

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

 

这里解释下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卸载依赖

 

 

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可执行文件。

配置完毕,打开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

更多技术文章分享请关注 公众号:软件测试小dao

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python 中,你可以使用工具来打包三方,以便于在其他项目中使用。下面是两种常用的打包工具: 1. setuptools:这是一个常用的打包工具,它可以帮助你创建和分发 Python 包。你可以使用 setuptools 来定义你的项目的依赖关系、入口点等信息,并生成一个可安装的分发包。你可以在项目的根目录下创建一个名为 `setup.py` 的文件,并使用 `setuptools.setup` 函数来配置你的项目。 以下是一个简单的 `setup.py` 文件示例: ```python from setuptools import setup setup( name='your_package_name', version='1.0', description='description of your package', author='your name', author_email='your@email.com', packages=['your_package'], install_requires=[ 'third-party-library==1.0', ], ) ``` 在命令行中执行 `python setup.py bdist_wheel` 命令可以生成一个 Wheel 文件(扩展名为 `.whl`)。这个文件可以通过 pip 来安装到其他项目中。 2. poetry:这是一个现代化的 Python管理工具,它不仅可以帮助你打包三方,还可以管理项目的依赖关系、虚拟环境等。你可以使用 poetry 来创建一个新项目,并在 pyproject.toml 文件中定义你的依赖关系。使用 `poetry build` 命令可以生成一个可安装的分发包。 以下是一个示例 pyproject.toml 文件: ```toml [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" [tool.poetry] name = "your_package_name" version = "1.0.0" description = "description of your package" authors = ["your name <your@email.com>"] [tool.poetry.dependencies] python = "^3.8" third-party-library = "^1.0" [tool.poetry.dev-dependencies] pytest = "^6.2" [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" ``` 在命令行中执行 `poetry build` 命令可以生成一个 Wheel 文件或者一个源码分发包(扩展名为 `.tar.gz`)。这些文件可以通过 pip 来安装到其他项目中。 这些工具提供了方便的方式来打包和分发第三方,你可以根据自己的需求选择使用其中之一。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值