|
14 | 14 | For the basic usage introduction we will be installing `pendulum`, a datetime library. |
15 | 15 | If you have not yet installed Poetry, refer to the [Introduction]({{< relref "docs" >}} "Introduction") chapter. |
16 | 16 |
|
17 | | -# Using Poetry in an Existing Project (For Contributors) |
18 | | - |
19 | | -If you are contributing to a project that already uses Poetry, you usually do not need to create a new project or initialize Poetry again. |
20 | | -In most cases, the project already contains a configured `pyproject.toml` file that defines its dependencies and development setup. Poetry can be used to quickly create a virtual environment that matches the project’s declared requirements. |
21 | | - |
22 | | -To install all project dependencies, navigate to the project directory and run the install command. This will create a virtual environment if one does not already exist and install all required dependencies. Development dependencies will also be installed if they are defined by the project. |
23 | | -Once the installation is complete, you can work inside the project’s virtual environment by activating it. Activating the environment opens a new subshell where all commands use the project’s configured Python and dependencies. |
24 | | - |
25 | | -Alternatively, if you prefer not to activate a subshell, you can run individual commands through Poetry. This allows you to execute Python, test runners, or other tools within the project environment without manually activating it. |
26 | | - |
27 | | -While contributing, you may need to add new dependencies. This can be done using Poetry’s dependency management commands, which will update the project configuration and lock file automatically. For development-only dependencies, the project may define a dedicated development dependency group. |
28 | | -Some projects may prefer contributors to edit the `pyproject.toml` file manually instead of adding dependencies via commands. Always follow the contribution guidelines of the project you are working on. |
29 | | -If your goal is to create a new Poetry-managed project rather than contribute to an existing one, refer to the sections below that describe project creation and initialization. |
30 | | - |
31 | | -# Install project dependencies |
32 | | -poetry install |
33 | | - |
34 | | -# Activate the virtual environment |
35 | | -poetry shell |
36 | | - |
37 | | -# Run commands within the environment |
38 | | -poetry run pytest |
39 | | - |
40 | | -# Add a new dependency |
41 | | -poetry add <package> --group dev |
42 | | - |
43 | 17 | ## Project setup |
44 | 18 |
|
45 | 19 | First, let's create our new project, let's call it `poetry-demo`: |
@@ -324,3 +298,35 @@ and update the lock file with the new versions. |
324 | 298 | Poetry will display a **Warning** when executing an install command if `poetry.lock` and `pyproject.toml` |
325 | 299 | are not synchronized. |
326 | 300 | {{% /note %}} |
| 301 | + |
| 302 | +## Contributing to an Existing Poetry Project |
| 303 | + |
| 304 | +If you are contributing to a project that already uses Poetry, you can skip/jump directly to this section: [Contributing to an Existing Project](#contributing-to-an-existing-poetry-project). |
| 305 | + |
| 306 | +To start contributing, follow these steps: |
| 307 | + |
| 308 | +```bash |
| 309 | +# Install all project dependencies |
| 310 | +poetry install |
| 311 | + |
| 312 | +# Activate the virtual environment |
| 313 | +poetry shell |
| 314 | + |
| 315 | +# Run commands inside the environment |
| 316 | +poetry run pytest |
| 317 | + |
| 318 | +# Add a new runtime dependency |
| 319 | +poetry add <package-name> |
| 320 | + |
| 321 | +# Add a new development-only dependency |
| 322 | +poetry add <package-name> --group dev |
| 323 | +Notes |
| 324 | +Virtual Environment: By default, Poetry creates a virtual environment in {cache-dir}/virtualenvs. You can also configure it to create the environment inside the project directory via poetry config virtualenvs.in-project true. |
| 325 | + |
| 326 | +Using your own virtual environment: If you manage a virtual environment externally, you can still use poetry run <command> inside it. Poetry will detect the activated environment automatically. |
| 327 | + |
| 328 | +Dependency management: Contributors typically rely on the existing pyproject.toml and poetry.lock. Only add dependencies if necessary, and commit the changes to the lock file to keep environments consistent. |
| 329 | + |
| 330 | +Reference: If you want to create a new project instead of contributing to an existing one, see the Project setup section above. |
| 331 | + |
| 332 | +{{% note %}} Poetry will display a Warning when executing an install command if poetry.lock and pyproject.toml are not synchronized. {{% /note %}} |
0 commit comments