poetry教程二(终结版)

您所在的位置:网站首页 poetry安装不了 poetry教程二(终结版)

poetry教程二(终结版)

2023-10-21 06:35| 来源: 网络整理| 查看: 265

思考

思考一个问题,在java工程中有pom.xml进行项目的打包编译部署版本控制,实现项目依赖隔离,在python工程中如何去做?

java

python

python中传统的版本及环境控制方式

老方法:通过pip安装,然后手动执行命令生成requirements.txt文件,通过virtualenv进行环境隔离,需要同时依赖两款工具,不能动态更新requirements.txt

升级版:pipenv解决了这个问题,集成pip的依赖包管理和virtualenv环境管理,维护pipfile来控制第三方包的依赖版本,能够自动更新记录文件

缺点:Lock速度缓慢,bug多,强行更新不相关依赖,依赖效果处理差,语法不清晰

python中传统的打包方式

你是否还在羡慕那些有开源项目的大佬,却不知道怎么推送自己的项目给其他人使用,还在使用github吗?还在纠结打包配置繁琐吗?他来了,他来了,他带着poetry走来了从此装逼不是大佬们的专属,我们也可以

 

 

from setuptools import setup, find_packages

import pathlib

here = pathlib.Path(__file__).parent.resolve()

long_description = (here / 'README.md').read_text(encoding='utf-8')

setup(

    name="opensourcetest",

    version="0.3.6",

    description='We need more free software interface testing.',

    long_description=long_description,

    long_description_content_type='text/markdown',

    url="https://github.com/chineseluo/opensourcetest",

    author='chineseluo',

    classifiers=[

        "Development Status :: 4 - Beta",         "Topic :: Software Development :: Testing",         "Topic :: Software Development :: Quality Assurance",         "Topic :: Software Development :: Libraries :: Python Modules",         "Operating System :: MacOS",         "Operating System :: POSIX :: Linux",         "Operating System :: Microsoft :: Windows",         "Programming Language :: Python :: 3.6",         "Programming Language :: Python :: 3.7",         "Programming Language :: Python :: 3.8",         "License :: OSI Approved :: Apache Software License"    ],

    keywords='HTTP, UI',

    package_dir={'': 'opensourcetest'},

    packages=find_packages(where='opensourcetest'),

    py_modules=["opensourcetest"],

    python_requires="~=3.6",

    install_requires=["pytest", "loguru", "PyYAML", "mkdocs"],

    entry_points={

        OST = "opensourcetest.cli:main"    

},

)

1、build

python setup.py build 将同级*.py文件拷贝到build下的同级目录中

2、打包

python setup.py sdist 打成一个tar.gz包

3、安装

a、解压

b、python setup.py install

setup.py帮助信息查看

python setup.py --help-commands

扩展:

1、制作windowns下安装包

python setup.py bdist_wininst # 创建"*.exe"的文件 python setup.py bdist_msi # 创建"*.msi"的文件 python setup.py bdist --format=msi # 同样是创建"*.msi"的文件 2、制作rpm包 python setup.py bdist_rpm # 创建"*.rpm"的文件,该命令需要在Linux操作系统上执行! python setup.py bdist --format=rpm # 同上 3、制作压缩文件 python setup.py bdist --format=zip # 创建"*.zip"压缩文件 python setup.py bdist --format=gztar # 创建"*.tar.gz"文件 4、将python打包成egg包或者whl包(zip) python setup.py bdist_egg # 打"*.egg"的包 python setup.py bdist_wheel # 打"*.whl"的包

为什么推荐使用poetry?

poetry是Python中依赖项管理和打包的工具。它允许您声明项目所依赖的库,它将为您管理(安装/更新)它们。官网地址:Introduction | Documentation | Poetry - Python dependency management and packaging made easy

使用标准的 pyproject.toml 文件,不用写多个配置文件 同时支持管理 Python 程序和 Python 库 更符合直觉的默认设计,比如不会随便更新锁定版本的依赖 干净简洁的命令行输出,没有星星和蛋糕 安装包的时候,使用 upper bound 版本限定,而不是 Pipenv 默认的通配符 卸载包的时候,直接卸载孤立的子依赖,不需要像 Pipenv 那样需要再执行 pipenv clean  

依赖管理,版本控制:

poetry可以控制python解释器的版本,依赖第三方包的版本

poetry add flask:安装最新稳定版本的flask

poetry add pytest --dev:指定为开发版本,会写到pyproject.toml中的[tool.poetry.dev-dependencies]区域

poetry add flask==2.22.0:指定具体的版本

poetry install:安装toml中全部的依赖

poetry install --no-dev:只安装非development环境的依赖

poetry update:更新所有锁定版本的依赖包

poetry update flask:更新指定依赖包

poetry remove flask:卸载依赖包

poetry show --outdated:查看可以更新的依赖

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

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

虚拟环境管理:

poetry可以生成虚拟环境,指定不同的python解释器版本,并且自由的切换。创建虚拟环境的两种方式:

1、如果在配置文件中配置了virtualenvs.create=true,执行poetry install时会检查是否有虚拟环境,否则会自动创建。

2、指定创建虚拟环境时使用的python解释器版本

poetry env use python3.8

常用命令

poetry shell:激活虚拟环境

 

poetry env info:显示虚拟环境列表

 

poetry env list --full-path:显示虚拟环境绝对路径

 

poetry env remove xxx:删除虚拟环境

 

poetry run python -v:查看python版本

如何使用poetry?

安装不讲述了,百度即可,参考Poetry教程一(Poetry安装与卸载)_成都 - 阿木木的博客-CSDN博客_poetry安装

新建一个poetry项目

poetry提供了脚手架帮我们创建项目的基本结构

执行poetry new opensourcetest

opensourcetest

├── pyproject.toml

├── README.rst

├── opensourcetest

│ └── __init__.py

└── tests

├── __init__.py

└── test_opensourcetest.py

初始化一个poetry项目

当项目本身已经是一个python项目,又想使用poetry时候,进入项目的根目录,执行poetry init即可

poetry.toml介绍

name :package 名字,必填

version :package 版本号  ,必填

description :package 描述  ,必填

license :package 许可证,可选

authors :package 作者,必填

maintainers :package 维护者,可选

readme :package readme 文件,可选,README.rst或README.md

homepage :package 项目网站的 URL,可选

repository :package 指向项目 repository 的 URL,可选

documentation :package 项目文档的 URL,可选

keywords 与 :package 相关的关键字列表(最多5个),可选

dependencies and dev-dependencies :默认情况下,poetry 会从 Pypi 库中查找依赖项,只需要写名称、版本就行

使用私有存储库

[[tool.poetry.source]]

name = 'private'

url = 'http://examplepypi.com/simplepypi'

extras 支持可选依赖项,装不装都行,当需要安装时,执行

poetry install --extras "pytest jmespath",或者 poetry install -E pytest -E jmespath

[tool.poetry.dependencies]

# 这些软件包是强制性的

mkdocs = "^1.0"

# 可选依赖项列表,可自行选择安装哪些

request = { version = "^2.7", optional = true }

pydantic = { version = "^1.3", optional = true }

[tool.poetry.extras]

mysql = ["mysqlclient"]

pgsql = ["psycopg2"]

[tool.poetry]

name = "opensourcetest"

version = "0.3.6"

description = "We need more free software interface testing."

license = "Apache-2.0"

readme = "README.md"

authors = ["chineseluo "]

homepage = "https://github.com/chineseluo/opensourcetest"

repository = "https://github.com/chineseluo/opensourcetest"

documentation = "http://docs.opensourcetest.cn"

keywords = ["HTTP", "http", "Http", "api","ui","UI", "automated testing", "interface", "test", "requests", "pytest", "allure", 'pytest-xdist']

classifiers = [

    "Development Status :: 4 - Beta",

    "Topic :: Software Development :: Testing",

    "Topic :: Software Development :: Quality Assurance",

    "Topic :: Software Development :: Libraries :: Python Modules",

    "Operating System :: MacOS",

    "Operating System :: POSIX :: Linux",

    "Operating System :: Microsoft :: Windows",

    "Programming Language :: Python :: 3.6",

    "Programming Language :: Python :: 3.7",

    "Programming Language :: Python :: 3.8",

    "License :: OSI Approved :: Apache Software License"

]

[tool.poetry.dependencies]

python = "^3.6"

pytest = "^5.2"

pytest-html = "^2.1.1"

loguru = "^0.5.3"

requests = "^2.22.0"

PyYAML = "^5.1.2"

mkdocs = "^1.1.2"

mkdocs-material = "^6.0.2"

jmespath = "^0.9.5"

pydantic = "^1.4"

allure-pytest = "^2.8.19"

selenium = "^3.141.0"

docker = "^4.4.0"

Appium-Python-Client = "^1.0.2"

[tool.poetry.dev-dependencies]

pytest = "^5.2"

loguru = "0.5.3"

requests = "2.24.0"

PyYAML = "5.3.1"

[tool.poetry.scripts]

OST = "opensourcetest.cli:main"

Ost = "opensourcetest.cli:main"

Opensourcetest = "opensourcetest.cli:main"

opensourcetest = "opensourcetest.cli:main"

[build-system]

requires = ["poetry-core>=1.0.0"]

build-backend = "poetry.core.masonry.api"

poetry.toml中版本依赖规范说明

给自己依赖的第三方包做一个版本限制,防止出现,因为第三方包版本更新,导致项目不可用等问题

^约束以左边第一位非0数字为主版本号,主版本号不能变

^约束规范

允许的版本范围

^1.2.3>=1.2.3 =1.2.0 =1.0.0 =0.2.3 =0.0.3 =0.0.0 =0.0.0 =1.2.3 =1.2.0 =1.0.0 =0.0.01.*>=1.0.0 =1.2.0 = 1.2.0

> 1

< 2

!= 1.2.3

确定的版本号或范围

>= 1.2,< 1.5

在依赖中可以指定项目的git依赖,默认拉取master分支,可以指定分支,commit hash,tag等

[tool.poetry.dependencies]

# Get the latest revision on the branch named "next"

requests = { git = "https://github.com/kennethreitz/requests.git", branch = "next" }

# Get a revision by its commit hash

flask = { git = "https://github.com/pallets/flask.git", rev = "38eb5d3b" }

# Get a revision by its tag

numpy = { git = "https://github.com/numpy/numpy.git", tag = "v0.13.2" }

本地路径依赖,如果需要依赖其他位于本地的项目,但是处于不同的路径,可以使用该方式

[tool.poetry.dependencies]

# directory

my-package = { path = "../my-package/", develop = false }

# file

my-package = { path = "../my-package/dist/my-package-0.1.0.tar.gz" }

远程url依赖,如果依赖远程仓库的文件,可以使用url的方式,使用poetry add的方式来添加url,

poetry add https://example.com/my-package-0.1.0.tar.gz

[tool.poetry.dependencies]

# directory

my-package = { url = "https://example.com/my-package-0.1.0.tar.gz" }

python版本限制依赖项

1、如果python版本不在约束范围内,就不会安装该依赖包

[tool.poetry.dependencies]

pathlib2 = { version = "^2.2", python = "~2.7 || ^3.2" }

2、根据python版本选择安装第三方包的版本

[tool.poetry.dependencies]

foo = [

    {version = "



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3