Skip to content
This repository was archived by the owner on Feb 15, 2026. It is now read-only.

Commit f5ff64f

Browse files
KemingHegithub-actions[bot]
authored andcommitted
docs(src/assets/): update poetry official documentation
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 45bcf3f commit f5ff64f

17 files changed

Lines changed: 7097 additions & 0 deletions

src/assets/poetry/_index.md

Lines changed: 412 additions & 0 deletions
Large diffs are not rendered by default.

src/assets/poetry/_metadata.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source_repo: python-poetry/poetry
2+
docs_path: docs
3+
updated_at: 2025-07-16T02:51:51Z
4+
commit_sha: 427d922a9e74a068ba68f8927460ec28a2442c8a

src/assets/poetry/basic-usage.md

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
---
2+
title: "Basic usage"
3+
draft: false
4+
type: docs
5+
layout: single
6+
7+
menu:
8+
docs:
9+
weight: 10
10+
---
11+
12+
# Basic usage
13+
14+
For the basic usage introduction we will be installing `pendulum`, a datetime library.
15+
If you have not yet installed Poetry, refer to the [Introduction]({{< relref "docs" >}} "Introduction") chapter.
16+
17+
## Project setup
18+
19+
First, let's create our new project, let's call it `poetry-demo`:
20+
21+
```bash
22+
poetry new poetry-demo
23+
```
24+
25+
This will create the `poetry-demo` directory with the following content:
26+
27+
```text
28+
poetry-demo
29+
├── pyproject.toml
30+
├── README.md
31+
├── src
32+
│ └── poetry_demo
33+
│ └── __init__.py
34+
└── tests
35+
└── __init__.py
36+
```
37+
38+
The `pyproject.toml` file is what is the most important here. This will orchestrate
39+
your project and its dependencies. For now, it looks like this:
40+
41+
```toml
42+
[project]
43+
name = "poetry-demo"
44+
version = "0.1.0"
45+
description = ""
46+
authors = [
47+
{name = "Sébastien Eustace", email = "sebastien@eustace.io"}
48+
]
49+
readme = "README.md"
50+
requires-python = ">=3.9"
51+
dependencies = [
52+
]
53+
54+
[build-system]
55+
requires = ["poetry-core>=2.0.0,<3.0.0"]
56+
build-backend = "poetry.core.masonry.api"
57+
```
58+
59+
Poetry assumes your package contains a package with the same name as `project.name` located in the root of your
60+
project. If this is not the case, populate [`tool.poetry.packages`]({{< relref "pyproject#packages" >}}) to specify
61+
your packages and their locations.
62+
63+
Similarly, the traditional `MANIFEST.in` file is replaced by the `project.readme`, `tool.poetry.include`, and
64+
`tool.poetry.exclude` sections. `tool.poetry.exclude` is additionally implicitly populated by your `.gitignore`. For
65+
full documentation on the project format, see the [pyproject section]({{< relref "pyproject" >}}) of the documentation.
66+
67+
### Setting a Python Version
68+
69+
{{% note %}}
70+
Unlike with other packages, Poetry will not automatically install a python interpreter for you.
71+
If you want to run Python files in your package like a script or application, you must _bring your own_ python interpreter to run them.
72+
{{% /note %}}
73+
74+
Poetry will require you to explicitly specify what versions of Python you intend to support, and its universal locking
75+
will guarantee that your project is installable (and all dependencies claim support for) all supported Python versions.
76+
Again, it's important to remember that -- unlike other dependencies -- setting a Python version is merely specifying which versions of Python you intend to support.
77+
78+
For example, in this `pyproject.toml` file:
79+
80+
```toml
81+
[project]
82+
requires-python = ">=3.9"
83+
```
84+
85+
we are allowing any version of Python 3 that is greater or equal than `3.9.0`.
86+
87+
When you run `poetry install`, you must have access to some version of a Python interpreter that satisfies this constraint available on your system.
88+
Poetry will not install a Python interpreter for you.
89+
90+
### Initialising a pre-existing project
91+
92+
Instead of creating a new project, Poetry can be used to 'initialize' a pre-populated
93+
directory. To interactively create a `pyproject.toml` file in directory `pre-existing-project`:
94+
95+
```bash
96+
cd pre-existing-project
97+
poetry init
98+
```
99+
100+
### Operating modes
101+
102+
Poetry can be operated in two different modes. The default mode is the **package mode**, which is the right mode
103+
if you want to package your project into an sdist or a wheel and perhaps publish it to a package index.
104+
In this mode, some metadata such as `name` and `version`, which are required for packaging, are mandatory.
105+
Further, the project itself will be installed in editable mode when running `poetry install`.
106+
107+
If you want to use Poetry only for dependency management but not for packaging, you can use the **non-package mode**:
108+
109+
```toml
110+
[tool.poetry]
111+
package-mode = false
112+
```
113+
114+
In this mode, metadata such as `name` and `version` are optional.
115+
Therefore, it is not possible to build a distribution or publish the project to a package index.
116+
Further, when running `poetry install`, Poetry does not try to install the project itself,
117+
but only its dependencies (same as `poetry install --no-root`).
118+
119+
{{% note %}}
120+
In the [pyproject section]({{< relref "pyproject" >}}) you can see which fields are required in package mode.
121+
{{% /note %}}
122+
123+
### Specifying dependencies
124+
If you want to add dependencies to your project, you can specify them in the
125+
`project` section.
126+
127+
```toml
128+
[project]
129+
# ...
130+
dependencies = [
131+
"pendulum (>=2.1,<3.0)"
132+
]
133+
```
134+
135+
As you can see, it takes a mapping of **package names** and **version constraints**.
136+
137+
Poetry uses this information to search for the right set of files in package "repositories" that you register
138+
in the `tool.poetry.source` section, or on [PyPI](https://pypi.org) by default.
139+
140+
Also, instead of modifying the `pyproject.toml` file by hand, you can use the `add` command.
141+
142+
```bash
143+
$ poetry add pendulum
144+
```
145+
146+
It will automatically find a suitable version constraint **and install** the package and sub-dependencies.
147+
148+
Poetry supports a rich [dependency specification]({{< relref "dependency-specification" >}}) syntax, including caret,
149+
tilde, wildcard, inequality and
150+
[multiple constraints]({{< relref "dependency-specification#multiple-constraints-dependencies" >}}) requirements.
151+
152+
## Using your virtual environment
153+
154+
By default, Poetry creates a virtual environment in `{cache-dir}/virtualenvs`.
155+
You can change the [`cache-dir`]({{< relref "configuration#cache-dir" >}} "cache-dir configuration documentation") value
156+
by editing the Poetry configuration.
157+
Additionally, you can use the
158+
[`virtualenvs.in-project`]({{< relref "configuration#virtualenvsin-project" >}}) configuration variable to create
159+
virtual environments within your project directory.
160+
161+
There are several ways to run commands within this virtual environment.
162+
163+
{{% note %}}
164+
**External virtual environment management**
165+
166+
Poetry will detect and respect an existing virtual environment that has been externally activated. This is a powerful
167+
mechanism that is intended to be an alternative to Poetry's built-in, simplified environment management.
168+
169+
To take advantage of this, simply activate a virtual environment using your preferred method or tooling, before running
170+
any Poetry commands that expect to manipulate an environment.
171+
{{% /note %}}
172+
173+
### Using `poetry run`
174+
175+
To run your script simply use `poetry run python your_script.py`.
176+
Likewise if you have command line tools such as `pytest` or `black` you can run them using `poetry run pytest`.
177+
178+
{{% note %}}
179+
If managing your own virtual environment externally, you do not need to use `poetry run` since
180+
you will, presumably, already have activated that virtual environment and made available the correct python instance.
181+
For example, these commands should output the same python path:
182+
183+
```shell
184+
conda activate your_env_name
185+
which python
186+
poetry run which python
187+
poetry env activate
188+
which python
189+
```
190+
191+
{{% /note %}}
192+
193+
### Activating the virtual environment
194+
195+
See [Activating the virtual environment]({{< relref "managing-environments#activating-the-environment" >}}).
196+
197+
## Version constraints
198+
199+
In our example, we are requesting the `pendulum` package with the version constraint `>=2.1.0 <3.0.0`.
200+
This means any version greater or equal to 2.1.0 and less than 3.0.0.
201+
202+
Please read [Dependency specification]({{< relref "dependency-specification" >}} "Dependency specification documentation")
203+
for more in-depth information on versions, how versions relate to each other, and on the different ways you can specify
204+
dependencies.
205+
206+
{{% note %}}
207+
**How does Poetry download the right files?**
208+
209+
When you specify a dependency in `pyproject.toml`, Poetry first takes the name of the package
210+
that you have requested and searches for it in any repository you have registered using the `repositories` key.
211+
If you have not registered any extra repositories, or it does not find a package with that name in the
212+
repositories you have specified, it falls back to PyPI.
213+
214+
When Poetry finds the right package, it then attempts to find the best match for the version constraint you have
215+
specified.
216+
{{% /note %}}
217+
218+
## Installing dependencies
219+
220+
To install the defined dependencies for your project, just run the [`install`]({{< relref "cli#install" >}}) command.
221+
222+
```bash
223+
poetry install
224+
```
225+
226+
When you run this command, one of two things may happen:
227+
228+
### Installing without `poetry.lock`
229+
230+
If you have never run the command before and there is also no `poetry.lock` file present,
231+
Poetry simply resolves all dependencies listed in your `pyproject.toml` file and downloads the latest version of their files.
232+
233+
When Poetry has finished installing, it writes all the packages and their exact versions that it downloaded to the `poetry.lock` file,
234+
locking the project to those specific versions.
235+
You should commit the `poetry.lock` file to your project repo so that all people working on the project are locked to the same versions of dependencies (more below).
236+
237+
### Installing with `poetry.lock`
238+
239+
This brings us to the second scenario. If there is already a `poetry.lock` file as well as a `pyproject.toml` file
240+
when you run `poetry install`, it means either you ran the `install` command before,
241+
or someone else on the project ran the `install` command and committed the `poetry.lock` file to the project (which is good).
242+
243+
Either way, running `install` when a `poetry.lock` file is present resolves and installs all dependencies that you listed in `pyproject.toml`,
244+
but Poetry uses the exact versions listed in `poetry.lock` to ensure that the package versions are consistent for everyone working on your project.
245+
As a result you will have all dependencies requested by your `pyproject.toml` file,
246+
but they may not all be at the very latest available versions
247+
(some dependencies listed in the `poetry.lock` file may have released newer versions since the file was created).
248+
This is by design, it ensures that your project does not break because of unexpected changes in dependencies.
249+
250+
### Committing your `poetry.lock` file to version control
251+
252+
#### As an application developer
253+
254+
Application developers commit `poetry.lock` to get more reproducible builds.
255+
256+
Committing this file to VC is important because it will cause anyone who sets up the project
257+
to use the exact same versions of the dependencies that you are using.
258+
Your CI server, production machines, other developers in your team,
259+
everything and everyone runs on the same dependencies,
260+
which mitigates the potential for bugs affecting only some parts of the deployments.
261+
Even if you develop alone, in six months when reinstalling the project you can feel confident
262+
the dependencies installed are still working even if your dependencies released many new versions since then.
263+
(See note below about using the update command.)
264+
265+
{{% warning %}} If you have added the recommended [`[build-system]`]({{< relref "pyproject#poetry-and-pep-517" >}}) section to your project's pyproject.toml then you _can_ successfully install your project and its dependencies into a virtual environment using a command like `pip install -e .`. However, pip will not use the lock file to determine dependency versions as the poetry-core build system is intended for library developers (see next section).
266+
{{% /warning %}}
267+
268+
#### As a library developer
269+
270+
Library developers have more to consider. Your users are application developers, and your library will run in a Python environment you don't control.
271+
272+
The application ignores your library's lock file. It can use whatever dependency version meets the constraints in your `pyproject.toml`. The application will probably use the latest compatible dependency version. If your library's `poetry.lock` falls behind some new dependency version that breaks things for your users, you're likely to be the last to find out about it.
273+
274+
A simple way to avoid such a scenario is to omit the `poetry.lock` file. However, by doing so, you sacrifice reproducibility and performance to a certain extent. Without a lockfile, it can be difficult to find the reason for failing tests, because in addition to obvious code changes an unnoticed library update might be the culprit. Further, Poetry will have to lock before installing a dependency if `poetry.lock` has been omitted. Depending on the number of dependencies, locking may take a significant amount of time.
275+
276+
If you do not want to give up the reproducibility and performance benefits, consider a regular refresh of `poetry.lock` to stay up-to-date and reduce the risk of sudden breakage for users.
277+
278+
### Installing dependencies only
279+
280+
The current project is installed in [editable](https://pip.pypa.io/en/stable/topics/local-project-installs/) mode by default.
281+
282+
If you want to install the dependencies only, run the `install` command with the `--no-root` flag:
283+
284+
```bash
285+
poetry install --no-root
286+
```
287+
288+
## Updating dependencies to their latest versions
289+
290+
As mentioned above, the `poetry.lock` file prevents you from automatically getting the latest versions
291+
of your dependencies.
292+
To update to the latest versions, use the `update` command.
293+
This will fetch the latest matching versions (according to your `pyproject.toml` file)
294+
and update the lock file with the new versions.
295+
(This is equivalent to deleting the `poetry.lock` file and running `install` again.)
296+
297+
{{% note %}}
298+
Poetry will display a **Warning** when executing an install command if `poetry.lock` and `pyproject.toml`
299+
are not synchronized.
300+
{{% /note %}}

0 commit comments

Comments
 (0)