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.
OS:macOS Catalina v10.15.4 poetry:v1.0.8
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
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.
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
.
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 the library from pyproject.toml
and also from the virtual environment.
poetry remove <package name>
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
#When updating only some packages
poetry update <package name>
#When updating all packages
poetry update
pyproject.toml
Used when you want to install packages from pyproject.toml
created by others at once
poetry install
poetry self: update
that appears occasionally when you search is like the command of poetry before v1.0, so please use the following command now.
poetry self update
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
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
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