[2020/06 latest version] Basic usage of python dependency management tool poetry

Introduction

Here is a very basic usage of poetry as of 2020/06. For the difference between poetry and other dependency tools and how to use them in more detail, please refer to the articles and official documents written by others.

Operation check environment

OS:macOS Catalina v10.15.4 poetry:v1.0.8

installation of poetry

Installation

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

Initial setting

If you type the following command, the virtual environment (.venv folder) created by poetry will be created directly under the project that manages packages using poetry.


poetry config virtualenvs.in-project true

The reason why this is necessary is that when using vscode, if this setting is not done, it is necessary to add the path of the virtual environment to vscode settings.json and take steps to make vscode recognize the virtual environment. However, if you set this setting first, you can save the trouble.

Introducing poetry into the project

Introduction

If you type the following command, pyproject.toml that manages the package list will be created directly under it.


poetry init

You will be asked interactively about the name of some projects, author, etc., so please answer accordingly. For the time being, it's okay to hit enter repeatedly. You can use poetry new when starting the project, but this command will create a directory and even a README. I think that you often want to create the directory structure yourself, so you can use poetry init.

Add package

Install in the virtual environment while writing the information of the library to be used in pyproject.toml created by poetry init.

#If you only need the latest one for the time being
poetry add <package name>

#Version can be specified
poetry add "pendulum>=2.0.5"

#Get it from git
poetry add git+https://github.com/sdispater/pendulum.git

Remove package

Remove the library from pyproject.toml and also from the virtual environment.

poetry remove <package name>

Execute command using virtual environment


poetry run <command>
#For example, when you want to start jupyterlab that you put in poetry with the package that you put in poetry enabled.
poetry run jupyter lab

Package update

#When updating only some packages
poetry update <package name>

#When updating all packages
poetry update

When installing the package described in pyproject.toml

Used when you want to install packages from pyproject.toml created by others at once

poetry install

Update of poetry itself


poetry self update

important point

Specifying the package name

If the package name specified at poetry init is the same as the package name you want to install with poetry add, the following error may occur.

pyproject.toml


[tool.poetry]
name = "kedro"


❯ poetry add kedro
Using version ^0.16.1 for kedro

Updating dependencies
Resolving dependencies... (0.0s)

[AssertionError]

In this case, change it to something else, such as name =" kedro_practice ", and you will be able to install normally. Reference issue: https://github.com/python-poetry/poetry/issues/1295

Specifying the python version

If you do not make any special settings when performing poetry init, the version of python that poetry expects will be described in pyproject.toml as follows.


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

What ^ 3.8 means is ** 3.8 <= python version <4.0 **. It is a version specification that assumes the maximum version where the numerical value representing the leftmost major version does not change. (See the official documentation (https://python-poetry.org/docs/versions/) for more information.) When installing a package with poetry add etc., poetry judges whether the installed package can be used with the python version set in pyproject.toml, but ** of the python to be judged The version will be all possible versions. ** In other words, in the above example, all versions that satisfy "3.8 <= python version <4.0", that is, ** v3.8 and v3.9 will be judged. ** ** For example, the package kedro supports 3.6, 3.7, 3.8 and does not support 3.9 as of 2020/06/07.

If you try to poetry add these packages withpython = "^ 3.8"in the[tool.poetry.dependencies]field of pyproject.toml, you will get the following error: ..


[SolverProblemError]
The current project's Python requirement (^3.8) is not compatible with some of the required packages Python requirement:
  - kedro requires Python >=3.6, <3.9

Because no versions of kedro match >0.16.1,<0.17.0
 and kedro (0.16.1) requires Python >=3.6, <3.9, kedro is forbidden.
So, because kedro-test depends on kedro (^0.16.1), version solving failed.

You're using version 3.8! Why not? !! You will end up killing time.

As a solution, you need to write pyproject.toml as below so that the version of python is only v3.8.

[tool.poetry.dependencies]
python = "^3.8,<3.9"
#Or
python = "=3.8"

For more information, follow poetry's github issue. https://github.com/python-poetry/poetry/issues/1413 https://github.com/python-poetry/poetry/issues/2444

at the end

It's not mentioned in this article, but it feels good to use in combination with pyenv. It's a lot more comfortable than pip, so please try it.

Recommended Posts

[2020/06 latest version] Basic usage of python dependency management tool poetry
Basic usage of Python f-string
[Introduction to Python] Basic usage of lambda expressions
Basic usage of flask-classy
Basic usage of Jinja2
Usage of Python locals ()
Basic knowledge of Python
python package dependencies and virtual environment management tool Poetry
[Introduction to Python] Basic usage of the library matplotlib
[Python] Correct usage of map
Put the latest version of Python on linux (Debian) on Chromebook
Super basic usage of pytest
Basic usage of PySimple GUI
Version upgrade of python Anaconda
Check OpenSSL version of python 2.6
Basic usage of Pandas Summary
Sample usage of Python pickle
[Python] Correct usage of join
A memorandum of understanding for the Python package management tool ez_setup
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
pyenv-change the python version of virtualenv
Ideone> Python version: 3.5 (as of August 29, 2017)
Change the Python version of Homebrew
Python package management tool personal summary
Basic grammar of Python3 system (dictionary)
[python] Correct usage of if statement
Basic study of OpenCV with Python
About the virtual environment of python version 3.7
Install Python package management tool pip (Windows)
Basic usage of Btrfs on Arch Linux
[Python] Try pydash of the Python version of lodash
Basic grammar of Python3 series (list, tuple)
Non-logical operator usage of or in python
Python Basic Course (at the end of 15)
Basic grammar of Python3 system (included notation)