File size: 4,699 Bytes
c0f13ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Poetry Usage Guide

This guide provides instructions on how to use [Poetry](https://python-poetry.org/) to manage dependencies, install packages, and prepare your project for both development and production environments.

## Table of Contents

- [Overview](#overview)
- [Installing Poetry](#installing-poetry)
- [Using Poetry in Development](#using-poetry-in-development)
  - [Installing Dependencies](#installing-dependencies)
  - [Updating Dependencies](#updating-dependencies)
  - [Adding and Removing Dependencies](#adding-and-removing-dependencies)
  - [Synchronizing Dependencies](#synchronizing-dependencies)
- [Using Poetry in Production](#using-poetry-in-production)
  - [Locking Dependencies](#locking-dependencies)
  - [Installing from `poetry.lock`](#installing-from-poetrylock)
- [Poetry Commands Summary](#poetry-commands-summary)

---

## Overview

Poetry is a dependency manager and build tool for Python projects. It simplifies managing dependencies, creating virtual environments, and ensuring version consistency between development and production environments. Poetry relies on two files:

- **`pyproject.toml`**: Defines the dependencies and configuration.
- **`poetry.lock`**: Locks dependencies to specific versions to ensure consistency.

---

## Installing Poetry(macOS only)

To install Poetry, use the following command:

```bash
brew install poetry
```

Refer to the [Poetry documentation](https://python-poetry.org/docs/#installation) for more options and OS-specific installation instructions.

---

## Using Poetry in Development

### Installing Dependencies

In development, install dependencies specified in `pyproject.toml`:

1. Navigate to the project directory:

   ```bash
   cd path/to/project
   ```

2. Run:
   ```bash
   poetry install
   ```

This command creates a virtual environment, installs all dependencies, and ensures they are compatible with the Python version specified.

### Updating Dependencies

During development, you can update dependencies by editing `pyproject.toml` directly and then running:

```bash
poetry install
```

This will apply any changes and update the environment without manually adding each dependency.

### Adding and Removing Dependencies

- **Add a New Dependency**:

  ```bash
  poetry add <package-name>
  ```

  Example:

  ```bash
  poetry add requests
  ```

- **Add a Development Dependency** (only used for development/testing):

  ```bash
  poetry add --group dev <package-name>
  ```

  Example:

  ```bash
  poetry add --group dev pytest
  ```

- **Remove a Dependency**:
  ```bash
  poetry remove <package-name>
  ```

### Synchronizing Dependencies

If the `pyproject.toml` or `poetry.lock` files are updated (e.g., after pulling changes), run:

```bash
poetry install
```

This keeps your environment synchronized with any updates made to the dependency files.

---

## Using Poetry in Production

### Locking Dependencies

To lock dependencies for production use, run:

```bash
poetry lock
```

This creates or updates `poetry.lock`, which pins each dependency to a specific version. This lock file should be used to maintain consistency in production.

### Installing from `poetry.lock`

In production, use `poetry.lock` to ensure exact dependency versions:

1. Install only the required (non-development) dependencies:
   ```bash
   poetry install --no-dev
   ```

This ensures that dependencies are installed exactly as defined in `poetry.lock`.

---

## Poetry Commands Summary

| Command                        | Description                                                   |
| ------------------------------ | ------------------------------------------------------------- |
| `poetry install`               | Installs dependencies from `pyproject.toml` or `poetry.lock`. |
| `poetry add <package-name>`    | Adds a new dependency and updates `pyproject.toml`.           |
| `poetry add --group dev <pkg>` | Adds a development-only dependency.                           |
| `poetry remove <package-name>` | Removes a dependency and updates `pyproject.toml`.            |
| `poetry update`                | Updates all dependencies to their latest compatible versions. |
| `poetry lock`                  | Locks dependencies to specific versions for production.       |
| `poetry shell`                 | Activates the Poetry-managed virtual environment.             |

---

## Additional Resources

- **Poetry Documentation**: [https://python-poetry.org/docs/](https://python-poetry.org/docs/)
- **GitHub Repository**: [https://github.com/python-poetry/poetry](https://github.com/python-poetry/poetry)

For further help, please refer to the [Poetry documentation](https://python-poetry.org/docs/).