crab

crab

The 'crabs in a barrel' mentality only exists if we view ourselves as a crab. The truth is on the map.

[重学Python] Poetry-Python 打包和依赖管理工具

Poetry-Python 打包和依赖管理工具#

重学 python, 在使用一段时间 python 之后,慢慢感觉以前的学习方法是不太对,所以开始整理重学 python 系列.


原则#

内容哪里来:

  • 内容基本都来官方文档和自己的实践总结
  • 介绍的东西确实好用和实用

能带来什么:

  • 给您带来的:花最少的时间掌握最常用的东西
  • 性价比高:学习成本不高 VS 收获不少

Poetry 是什么#

PYTHON PACKAGING AND DEPENDENCY MANAGEMENT MADE EASY


Python 项目管理#

  • 创建虚拟环境
  • 使用 requirements.txt
      pip install <name> 
      pip freeze -r \
        > requirements.txt
    
  • setup.py 打包

Poetry 的好处#

  • 集成了上面所有功能
  • 统一管理依赖和打包
  • 方便使用的命令行工具
  • 可以集成 shell 命令自动完成功能

Poetry 创建项目#

poetry new pydaily
tree pydaily


Poetry 虚拟环境#

poetry shell

Poetry 添加依赖#

poetry add requests
poetry add pytest -D

Poetry 项目文件#

  • pyproject.toml: 项目依赖,打包发布生声明
  • poetry.lock: 确定项目安装内容
  • 类似于 Javascript 的:
    • package.json
    • package.lock

Poetry: pyproject.toml#

pyproject.toml 文件实际就是 node 的:

  1. package.json 文件
  2. java 中的 maven,gradle 文件

Poetry 常用命令#

  • poetry add : 添加依赖
  • poetry install: 安装你的代码 - pip install
  • poetry update: 更新所有依赖
  • poetry run: 运行命令
  • poetry build: 打包
  • poetry publish: 发布包

Poetry 使用的好处#

  • 集成 python 从虚拟环境,第三方依赖,打包和发布功能
  • 一个工具搞定所有项目配置
  • 使用和其他语言理念一致
    • npm package.json
    • maven/gradle: pom.xml/build.gradle
    • go: go.mod

Poetry 依赖管理 -- git 仓库管理#

  • 依赖可以是 git 仓库,本地文件
[tool.poetry.dependencies]
# Get the latest revision on the branch named "next"
requests = { git = "https://github.com/kennethreitz/requests.git", branch = "next" }

Poetry 依赖管理 -- 从 git 安装#

poetry add "https://github.com/myorg/mypackage_with_subdirs.git#subdirectory=subdir"

Poetry 依赖管理 - 本地文件#

[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" }

Poetry 使用教程视频#

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。