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 使用教程視頻#

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。