Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

License

NotificationsYou must be signed in to change notification settings

toraritte/poetry-intro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction to thePoetry tool for Python projects

1. What isPoetry?

Poetry is modestly described byits official documentation as adependency management tool, but it is in fact emerging as a de facto standard) to

[†]: QuotingHow to manage Python projects with Poetry

  • control virtual environments

  • create and publish packages/libraries

  • quickly set up new Python projects (or to easily convert existing ones)

2. Why use it?

Even though the Python eco-system is rich with internal (venv,pyvenv) and external tools (pip,pyenv,pipenv,virtualenv, etc.) that take on one or two of the roles listed above, this diversity can become an issue:

Poetry aims to consolidate the functionality of all these tools (TODO: paraphrase,https://www.software.com/src/poetry-can-redefine-the-future-of-python), while striving to stay easy to use and configure.

[†]: ParaphrasingIs Poetry the future of Python?

3. UsingPoetry withNix

Nix is across-platform system package manager supported on most major distributions of Unix derivative operating systems (Linux, BSDs, MacOS, etc.) but it is not available on Windows.

The title of the section is deliberately not called "Installation" also because the"Installation" section of thePoetry documentation comprehensively covers this topic for operating systemsnot managed withNix (the reason I elected to demonstrate usingPoetry withNix in the first place), and becauseNix allows using software (and software components) in a way that doesn't conform to the traditional use of the term "installation".

3.1 "Ad hoc" use withnix-shell

$ nix-shell -p python3 poetry

nix-shell will start a sub-shell with the latest Python 3 andPoetry executables, and one can start hacking.

NOTE: Once exiting the sub-shell,python3 andpoetry are still available on the system (seewhich poetry andwhich python3) but they are not referenced in thePATH environment variable. Hence they will need to be aliased or called by their full path. (Packages used withnix-shell are also subject togarbage collection after leaving the sub-shell.)

nix-shell can also be used to set up a more elaborate development environment, and this is probably the most flexible and convenient way.

ASIDE: The blog postDeveloping Python with Poetry &poetry2nix: reproducible flexible Python environments nicely expands on this topic.

3.2 Imperative installation withnix-env

$ nix-env -i python3 poetry

The only difference between with this one-liner and the one in the previous section is thatpython3 andpoetry are now available inPATH, and they won't be garbage collected (unless removed with [nix-env -e] first).

TODO:nix-env -i poetry fails witherror: selector 'poetry' matches no derivations; related to channels?

3.3 Declarative installation

WARNING: This is a more advanced (and sprawling)Nix topic, and I still believe that the most convenient way is usingnix-shell, but it's mentioned here for completeness sake.

The following are the best places to start with, depending

Nix also offers a lot of freedom on how to do things, and this can complicate things, therefore the following resources are also suggested for reading:

4. Life cycle of aPoetry project

There are many "getting started" guides out there (e.g.,1,2,3,4,5,6), therefore I was striving to show (a hint of) the big picture.

Starting a
 new Python project
from scratch?
Starting a...
Yes
Yes
No
No
poetry new
poetry new
poetry init
poetry init
Specify and install dependencies

  Directly: 1. Edit{project-root}/             
 

  
Interactively: 
Specify and install dependencies...
pyproject.toml
pyproject.toml
poetry add
poetry add
In the root of an existing projectto start managing it with Poetry.
In the root of an existing...
Set up a new Python project managed with Poetry via an interactive  questionnaire. 

See directory template as well.
Set up a new Python project...2. poetry installSee intro to specifyi...

poetry update
poetry update
Update dependencies without version constraints (for others,the constraints have to be updated first).
Update dependencies without vers...
time passes
time p...
Is existing
project already
managed with
Poetry?
Is existing...
Yes
Yes
No
No
poetry install
poetry install
E.g., project clone from an online repo, moved from another machine, etc.
E.g., project clone from an...
poetry remove
poetry remove

Other dependency management commands:
Other dependency management commands:...
After editing pyproject.toml, it is recommended to usepoetry check.
After editing pyproject.toml...

poetry run
poetry run
poetry shell
poetry shell
Develop
Develop
... and run/test the project maybe with
... and run/test the proj...
Amend dependencies
Amend...
Publish package
Publish package
Re-fit project parameters
Re-fit project parameters
poetry env
poetry env
poetry config
poetry config
time passes
time p...
poetry configalways creates a global configuration file if one is not present.
poetry config always creates a...
poetry build
poetry build
poetry publish
poetry publish
E.g., project clone from an online repo, moved from another machine, etc.
E.g., project clone from an...
poetry publish --build
poetry publish --buildViewer does not support full SVG 1.1

5.Poetry's configuration and virtual environment management

WARNING: Please take any info in this section with grain of salt: put this together based on theofficial documentation, but I'm a new to Python, and it hasn't been reviewed by more experienced users at all.

The flowchart below shows howPoetry'sconfiguration options come into play when

Is local config file
{project-root}/poetry.toml
present?
Is local config file...
Use it
Use it
Location not configurable
Location not configurable
Poetry command
Poetry command
Use the global configuration file
(for Linux: ~/.config/pypoetry/config.toml)
Use the global configuration file...
To create/manage a project-local config file, use
To create/manage a project-local config file, use...
Yes
Yes
No
NoIs project running in a virtual environment?
virualenvs.create
virualenvs.create
Is pip installed?
Is pip installed?
false
false
No
No
Runtime error at one point? It should be there according to the note
Runtime error at one point? It...
  ?
  ?
Given that the default value ofvirtualenvs.in-projectboolean configuration option is None, this is a valid concern. See the table at the bottom as well.
Given that the default value of virtualenvs.in-project...
Yes
Yes
true
true

virtualenvs.in-project

{true, false}
virtualenvs.in-project...
Yes
Yes
No
No

{project-root}/.venv
present?
{project-root}/.venv...
Yes
Yes
Use it
Use it
No
No
Create .venv
 invirtualenvs.path
Create .venv...virtualenvs.in-project...
other
other
false
false

{project-root}/.venv
present?
{project-root}/.venv...
Yes
Yes
No
No
Ignore it
Ignore itIs project running in a virtual environment?
Use {project-root}/.venv
if present, create it otherwise.
Use {project-root}/.venv...
true
true
Use Python installed with Pip?
Use Python installed with Pip?
Is activated
Python version compatible
with the project?
Is activated...
Not running in a virtual environment
Not running i...
Yes, running in a virtual environment
Yes, running in...
What Python executable
 to use?
What Python executable...
Yes
Yes
Use it
Use it
Is there a
compatible version?
Is there a...
No
No
Yes
Yes
Switch to it
Switch to it
No
No
STOP, and ask user to configure one manually (e.g., pyenv,poetry env)
STOP, and ask user to configure...
That is,
That is,...Viewer does not support full SVG 1.1

The above flowchart may not convey clearly howPoetry chooses the virtual environment to use so this table is to provide an alternative representation:

virtualenvs.createvirtualenvs.in-project{project-root}/.venv presentexpected behavior
true (default)trueyesuse{project_root}/.venv
nocreate{project_root}/.venv
falseyesignore{project_root}/.venv and use/create global one atvirtualenvs.path
nouse/create global one atvirtualenvs.path
other
(∉ {true,false})
yesuse{project_root}/.venv
no(?) use/create global one atvirtualenvs.path
false
(andpip is available)
trueyesI presume that as virtual environments are disabled, the rest depends on the active Python version andpip.
no
falseyes
no
other
(∉ {true,false})
yes
no
other
(∉ {true,false})
trueyesGiven that the default value ofvirtualenvs.in-projectboolean configuration option is None, this I presume thatvirtualenvs.create may also be given a value other thantrue orfalse - although unsure what [Poetry]'s behaviour will be.
no
falseyes
no
other
(∉ {true,false})
yes
no

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp