首页 > 编程语言 >Python Poetry 依赖管理工具

Python Poetry 依赖管理工具

时间:2023-08-13 16:22:49浏览次数:34  
标签:依赖 group virtualenvs Python poetry Poetry -- add 管理工具

Python 依赖管理工具 poetry

安装

Linux, macOS, Windows (WSL)

curl -sSL https://install.python-poetry.org | python3 -

Windows (Powershell)

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

配置

使用 poetry config --list 来查看配置

PS C:\Users\lingh\Desktop\poetry-demo> poetry config --list
cache-dir = "C:\\Users\\lingh\\AppData\\Local\\pypoetry\\Cache"
experimental.system-git-client = false
installer.max-workers = null
installer.modern-installation = true
installer.no-binary = null
installer.parallel = true
repositories.tsinghua.url = "https://pypi.tuna.tsinghua.edu.cn/simple"
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}\\virtualenvs"  # C:\Users\lingh\AppData\Local\pypoetry\Cache\virtualenvs
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{project_name}-py{python_version}"
PS C:\Users\lingh\Desktop\poetry-demo>

默认 poetry 会在 {cache-dir}\virtualenvs 创建虚拟环境,可以修改为创建在每个项目的目录下

PS C:\Users\lingh\Desktop\poetry-demo> poetry config virtualenvs.in-project true
PS C:\Users\lingh\Desktop\poetry-demo>

新建项目

使用 poetry new 来创建项目

poetry new poetry-demo

进入项目目录

cd poetry-demo

添加清华源,这里还需要添加 pypi 不然会有一个 warning

poetry source add tsinghua https://pypi.tuna.tsinghua.edu.cn/simple
poetry source add pypi

添加依赖,可以分组

poetry add requests
poetry add pytest --group test

使用 pycharm 打开项目

pycharm 会自动检测并使用项目目录下 .venv 的解释器,如果不是,需要手动配置一下

配置 pycharm 使用 pytest 进行测试
Testing -> Default test runner 选 pytest

这样就可以执行测试了

也可以使用 poetry 来执行测试

poetry run pytest

Poetry 依赖管理

poetry 通过 group 来进行依赖管理
运行时依赖放在 [tool.poetry.dependencies] 下面
测试需要的依赖可以放在 [tool.poetry.group.test.dependencies] 下面

[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.31.0"


[tool.poetry.group.test.dependencies]
pytest = "^7.4.0"

开发需要的依赖可以放在 [tool.poetry.group.dev.dependencies] 下面

[tool.poetry.group.dev.dependencies]
pytest = "^6.0.0"
pytest-mock = "*"

使用 optional 来指定可选依赖,只有在明确指定时才会去下载

[tool.poetry.group.docs]
optional = true

[tool.poetry.group.docs.dependencies]
mkdocs = "*"

使用 --with 下载可选依赖

poetry install --with docs

添加依赖

不加 group 时添加到 [tool.poetry.dependencies] 下面

poetry add requests

使用 --group 来添加到对应的分组下

poetry add pytest --group test

安装依赖

poetry install

如果有可选依赖需要通过 --with 来安装

poetry install --with test,docs

删除依赖

poetry remove requests
poetry remove mkdocs --group docs

poetry 命令列表

项目命令

创建项目

poetry new my-package

创建的项目结构如下

my-package
├── pyproject.toml
├── README.md
├── src
│   └── my_package
│       └── __init__.py
└── tests
    └── __init__.py

现有项目 使用 poetry
在项目根目录执行 poetry init 会生成 pyproject.toml 之后就可以采用 poetry 来进行依赖管理

poetry init

依赖命令

安装依赖

poetry install

更新依赖
默认时 poetry 会使用 poetry.lock 文件锁定依赖库的版本,需要更新时执行 poetry update

poetry update

添加依赖

poetry add requests pendulum

添加依赖时指定版本约束

# Allow >=2.0.5, <3.0.0 versions
poetry add pendulum@^2.0.5

# Allow >=2.0.5, <2.1.0 versions
poetry add pendulum@~2.0.5

# Allow >=2.0.5 versions, without upper bound
poetry add "pendulum>=2.0.5"

# Allow only 2.0.5 version
poetry add pendulum==2.0.5

可以添加 git 私有依赖

poetry add git+https://github.com/sdispater/pendulum.git
poetry add git+https://github.com/sdispater/pendulum.git#develop
poetry add git+https://github.com/sdispater/pendulum.git#2.0.5

删除依赖

poetry remove pendulum

显示依赖

PS C:\Users\lingh\Desktop\poetry-demo> poetry show
certifi            2023.7.22 Python package for providing Mozilla's CA Bundle.
charset-normalizer 3.2.0     The Real First Universal Charset Detector. Open, modern and actively maintained alter...
colorama           0.4.6     Cross-platform colored terminal text.
exceptiongroup     1.1.2     Backport of PEP 654 (exception groups)
idna               3.4       Internationalized Domain Names in Applications (IDNA)
iniconfig          2.0.0     brain-dead simple config-ini parsing
packaging          23.1      Core utilities for Python packages
pluggy             1.2.0     plugin and hook calling mechanisms for python
pytest             7.4.0     pytest: simple powerful testing with Python
requests           2.31.0    Python HTTP for Humans.
tomli              2.0.1     A lil' TOML parser
urllib3            2.0.4     HTTP library with thread-safe connection pooling, file post, and more.
PS C:\Users\lingh\Desktop\poetry-demo>

搜索依赖

PS C:\Users\lingh\Desktop\poetry-demo> poetry search requests

requests (2.31.0)
 Python HTTP for Humans.

导出依赖,可以导出 pip 格式的依赖

poetry export -f requirements.txt --output requirements.txt

配置命令

配置

poetry config --list

修改配置

poetry config virtualenvs.in-project true

运行命令

在虚拟环境里面执行

poetry run python -V

缓存

列出缓存

poetry cache list

清除缓存

poetry cache clear pypi --all

添加源

poetry source add tsinghua https://pypi.tuna.tsinghua.edu.cn/simple

添加源时指定优先级 --priority

poetry source add --priority=explicit pypi

源的优先顺序

  • default
  • primary
  • implicit PyPI (当有 default 或被指定为 explicit 时无效)
  • supplemental sources
  • explicit 只有依赖指定这个源名称时,才会从 explicit 源下载

添加依赖时指定源名称

poetry add --source internal-pypi httpx

显示源

poetry source show

显示依赖的源

poetry source show pypi-test

删除源

poetry source remove pypi-test
---- ---- ----

标签:依赖,group,virtualenvs,Python,poetry,Poetry,--,add,管理工具
From: https://www.cnblogs.com/goallin/p/17626700.html

相关文章

  • python中类和对象的关系
    引用.属性会先在对象里面找,如果找不到该属性则会在对应的类里面找(__class__相连接)   ......
  • How to compare two linked lists are equal in Python All In One
    HowtocomparetwolinkedlistsareequalinPythonAllInOne在Python中如何比较两个链表是否相等#Definitionforsingly-linkedlist.fromtypingimportOptionalclassListNode:def__init__(self,val=0,next=None):self.val=valself.next=......
  • How to use Javascript JSON.stringify similar method in Python All In One
    HowtouseJavascriptJSON.stringifysimilarmethodinPythonAllInOne如何在Python中使用类似JavaScriptJSON.stringify的方法应用场景比较两个数组(列表)对象是否相等/comparestwoarray(list)objectsforequality//jsarr1=[1,2,3]arr2=[1,2,3]......
  • Python - 会考必过篇
    文章简介该文章是为了帮助身边朋友为了会考信息与技术将Python这门语言给学会而创作的。开发工具在线运行环境腾讯Python实验室:Python|Coding1024Code:Python|1024CodeLwebapp:Python|Lwebapp!>以上在线运行环境,前两种需要登录,目前找到的仅有第三种比较好用,......
  • Python 农历公历相互转换
    Python农历公历相互转换stackOverflowsh关注IP属地:江苏0.2252019.02.1918:23:48字数2,054阅读6,862背景日常用python处理各种数据分析工作,最近需要对历年春节期间的数据做一些对比工作,本来只是用了一个简单的日期数组来进行,但后来发现一些数据在农历日期进行对比......
  • Python代码分享-获取B站粉丝数量
    分享一下一段Python代码。#获取B站粉丝数量#冰河之刃#2020-08-02importrequestsimportjsonurl="https://api.bilibili.com/x/relation/stat?vmid=490458635"payload={}#发起请求response=requests.request("GET",url,data=payload)#print(response.......
  • 学习笔记-流畅的Python 1st
    P31和*都遵循不修改原有的操作对象,而是创建一个新的序列>>>a=[1,2,3]>>>c=a*2>>>a[0]=3>>>c[1,2,3,1,2,3]如果在a*n这个语句中,序列a里的元素是对其他可变对象的引用的话,你就需要格外注意了,因为这个式子的结果可能会出乎意料。比如,你想用my......
  • 4.0 Python 变量与作用域
    在python中,变量的作用域决定了变量在哪些位置可以被访问。一个程序中的变量并不是所有的地方都可以访问的,其访问权限决定于变量的赋值位置。python中有两种最基本的变量作用域:局部作用域和全局作用域。局部变量是在函数内部定义的变量,只能在其被声明的函数内部访问。而全局变量则......
  • 5.0 Python 定义并使用函数
    函数是python程序中的基本模块化单位,它是一段可重用的代码,可以被多次调用执行。函数接受一些输入参数,并且在执行时可能会产生一些输出结果。函数定义了一个功能的封装,使得代码能够模块化和组织结构化,更容易理解和维护。在python中,函数可以返回一个值或者不返回任何值,而且函数的参......
  • n、Appium_Python_Api
    一、Appium_Python_Api方法参考博客:https://blog.csdn.net/ezreal_tao/article/details/80911950https://cloud.tencent.com/developer/article/1569596contextscontexts(self):Returnsthecontextswithinthecurrentsession.返回当前会话中的上下文,使用后可以识别H5......