crab

crab

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

[Relearning Python] Poetry-Python Packaging and Dependency Management Tool

Poetry-Python Packaging and Dependency Management Tool#

Relearning Python, after using Python for some time, I gradually realized that my previous learning methods were not quite right, so I started organizing the relearning Python series.


Principles#

Where does the content come from:

  • The content mostly comes from official documentation and my own practical summaries
  • The things introduced are truly useful and practical

What can it bring:

  • To bring you: Spend the least amount of time mastering the most commonly used things
  • High cost-effectiveness: Low learning cost vs. considerable gains

What is Poetry#

PYTHON PACKAGING AND DEPENDENCY MANAGEMENT MADE EASY


Python Project Management#

  • Creating a virtual environment
  • Using requirements.txt
      pip install <name> 
      pip freeze -r \
        > requirements.txt
    
  • Packaging with setup.py

Benefits of Poetry#

  • Integrates all the above functions
  • Unified management of dependencies and packaging
  • Convenient command-line tool
  • Can integrate shell command auto-completion feature

Creating a Project with Poetry#

poetry new pydaily
tree pydaily


Poetry Virtual Environment#

poetry shell

Adding Dependencies with Poetry#

poetry add requests
poetry add pytest -D

Poetry Project Files#

  • pyproject.toml: Project dependencies, packaging and publishing declarations
  • poetry.lock: Determines the project installation content
  • Similar to:
    • package.json in JavaScript
    • package.lock in JavaScript

Poetry: pyproject.toml#

The pyproject.toml file is actually like:

  1. package.json file in Node.js
  2. Maven or Gradle file in Java

Common Commands in Poetry#

  • poetry add: Add dependencies
  • poetry install: Install your code (equivalent to pip install)
  • poetry update: Update all dependencies
  • poetry run: Run a command
  • poetry build: Build the project
  • poetry publish: Publish the package

Benefits of Using Poetry#

  • Integrates Python's virtual environment, third-party dependencies, packaging, and publishing functions
  • One tool to handle all project configurations
  • Consistent with the concepts of other languages
    • npm package.json
    • Maven/Gradle: pom.xml/build.gradle
    • Go: go.mod

Poetry Dependency Management - Git Repository Management#

  • Dependencies can be git repositories or local files
[tool.poetry.dependencies]
# Get the latest revision on the branch named "next"
requests = { git = "https://github.com/kennethreitz/requests.git", branch = "next" }

Poetry Dependency Management - Installing from Git#

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

Poetry Dependency Management - Local Files#

[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 Tutorial Videos#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.