poetry - Python environment management tool

The use of poetry in Python package management

Poetry is a Python virtual environment and dependency management tool. I used pipenv before. I came into contact with poetry when learning httprunner recently. Poetry is similar to pipenv, and also provides packaging and publishing functions.
Official documents: https://python-poetry.org/docs/

python project deployment: poetry manages the local environment, and docker is used online

1, poetry installation

poetry provides a variety of installation methods. I recommend choosing from the following two methods:

Method 1: (official recommendation)

# Linux
$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
# Prompt after installation
It will add the `poetry` command to Poetry's bin directory, located at:

$HOME/.poetry/bin

This path will then be added to your `PATH` environment variable by
modifying the profile file located at:

$HOME/.profile

You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.

Installing version: 1.1.6
  - Downloading poetry-1.1.6-linux.tar.gz (72.33MB)

Poetry (1.1.6) is installed now. Great!

To get started you need Poetry's bin directory ($HOME/.poetry/bin) in your `PATH`
environment variable. Next time you log in this will be done
automatically.

To configure your current shell run `source $HOME/.poetry/env`
# Use the command source $home / Poetry / env add environment variable
# restart

# windows waited a long time
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
# After installation, the result is similar to Linux
# Add% userprofile% \. To the environment variable poetry\bin

Mode 2: (pip)

$ pip install --user poetry

Use the command to see if the installation was successful

$ poetry --version

2, Project initialization

1. If you have not created an environment yet, you can use the poetry environment

Enter "environment name" to create a project scaffold, including the basic structure pyproject Toml file.

$ poetry new poetry-demo

At this time, a project containing the following contents will be created,

poetry-demo
	├── pyproject.toml
	├── README.rst
	├── poetry_demo
		│── __init__.py
	├── tests
	├── __init__.py
	└── test_poetry_demo.py

2. If a new project is created, it can also be created based on the existing project

$ poetry init

At this time, it will let you ENTER the package name, version number and other information. You can choose to ENTER or press ENTER to use the default value. After completion, pyproject Toml is in the following format:

name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["xxxxx"]

[tool.poetry.dependencies]
python = "*"

[tool.poetry.dev-dependencies]
pytest = "^3.4" # Specify that this package can only be 3.4

3, Dependent package management

Install dependent packages

You can use the install command to directly parse and install pyproject Dependent packages of toml

$ poetry install

#pyproject. The configuration of toml file is as follows:
	# [tool.poetry.dependencies]
	# pendulum = "^1.4"

You can also use the add command to install a Python toolkit,

$ poetry add numpy

You can also add the configuration parameter – dev to distinguish dependent packages in different environments.

Details:

Poetry add flash: install the latest stable version of flash
poetry add pytest --dev: specify as development dependency and write to pyproject [tool. Poetry. Dev dependencies] area in toml
poetry add flask=2.22.0: specify the specific version
poetry install: install pyproject All dependencies in the toml file
Poetry install -- no dev: only the dependencies of non development environment are installed, which is generally used during deployment

Update dependent packages for all locked versions

$ poetry update

Update specified dependent packages

$ poetry update numpy

Uninstall dependent packages

$ poetry remove numpy

View dependencies that can be updated

$ poetry show --outdated

View project installation dependencies

$ poetry show

View project installation dependencies in a tree structure

$ poetry show -t

4, Virtual environment management

Create virtual environment
There are two ways to create a virtual environment:

Mode 1:

If virtualenvs. Is configured in the configuration file Create = true, check whether there is a virtual environment when executing poetry install, otherwise it will be created automatically.

Mode 2:

Specifies the Python interpreter version to use when creating the virtual environment

$ poetry env use python3.7

Activate virtual environment

$ poetry shell

View virtual environment information

$ poetry env info

Displays a list of virtual environments

$ poetry env list

Show virtual environment path

$ poetry env list --full-path

Delete virtual environment

$ poetry env remove python3.7

View python version

$ poetry run python -V

Change configuration

# Change environment installation path
$ poetry config virtualenvs.path /path/to/cache/directory/virtualenvs

Keywords: Python

Added by Brandito520 on Wed, 26 Jan 2022 09:42:46 +0200