Skip to content

Commit

Permalink
Add more content to pdm page.
Browse files Browse the repository at this point in the history
  • Loading branch information
redjax committed Mar 1, 2024
1 parent 00efdca commit c34fa7d
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions docs/programming/python/pdm.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
- [x] Write section describing `pdm`
- [x] What is `pdm`?
- [x] What problem does `pdm` solve?
- [ ] Write `pdm` cheat sheet
- [x] Write `pdm` cheat sheet
- [x] Initialize a project
- [x] Difference between an `application` and a `library`
- [ ] Add/remove dependencies
- [ ] Execute code
- [ ] Add development dependencies
- [ ] Publish project to `pypi`
- [x] Add/remove dependencies
- [x] Execute code
- [x] Add development dependencies
- [ ] Detail publishing a project to `pypi` with `pdm

!!! TOC

Expand All @@ -23,25 +23,25 @@

## PDM cheat sheet

!!! TODO

- [ ] Install dependency
- [ ] Install dependency in a specific group
- [ ] "development" group
- [ ] A custom "standard" group
- [ ] Uninstall dependency
- [ ] Uninstall dependency from a specific group
- [ ] "development" group
- [ ] A custom "standard" group
- [ ] Update a package
- [ ] Lock dependencies
- [ ] Run commands with `pdm run`
- [ ] Execute a PDM script
- [ ] Publish a package to pypi

| Command | Description | Notes |
| ---------- | ----------------------------------- | ----------------------------------------------------------------------------------------------- |
| `pdm init` | Initialize a new project with `pdm` | The `pdm` tool will ask a series of questions, and build an environment based on your responses |
| Command | Description | Notes |
| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `pdm init` | Initialize a new project with `pdm` | The `pdm` tool will ask a series of questions, and build an environment based on your responses |
| `pdm add <package-name>` | Add a Python package to the project's dependencies. | `pdm` will automatically find all required dependencies and add them to the install command, and will ensure the package installed is compatible with your environment. |
| `pdm add -d <package-name>` | Add a Python package to the project's `development` dependency group. | Use this for packages that should not be built with your Python package. Useful for things like formatters (`black`, `ruff`, `pyflakes`, etc), testing suites (`pytest`, `pytest-xidst`), automation tools (`nox`, `tox`), etc. |
| `pdm add -G standard <package-name>` | Add a Python package to the project's `standard` group. | In this example, `standard` is a custom dependency group. You can use whatever name you want, and users can install the dependencies in this group with `pip install <project-name>[standard]` (or with `pdm`: `pdm add <project-name>[standard]`) |
| `pdm remove <package-name>` | Remove a Python dependency from the project. | Analagous to `pip uninstall <package-name>` |
| `pdm remove -d <package-name>` | Remove a Python development dependency from the project. | |
| `pdm remove -G standard <package-name>` | Remove a Python dependency from the `standard` (or whatever group name you're targeting) dependency group. | `standard` is an example name. You can use whatever name you want for dependency groups, as long as they adhere to the naming rules. You will be warned if a name is not compatible. |
| `pdm update` | Update all dependencies in the `pdm.lock` file. | |
| `pdm update <package-name>` | Update a specific dependency. | |
| `pdm update -d <package-name>` | Update a specific development dependency. | |
| `pdm update -G standard <package-name>` | Update a specific dependency in the "standard" dependency group. | `standard` is an example name; make sure you're using the right group name. |
| `pdm update -d` | Update all development dependencies. | |
| `pdm update -G standard` | Update all dependencies in the dependency group named "standard". | |
| `pdm update -G "standard,another_group"` | Update multiple dependency groups at the same time. | |
| `pdm lock` | Lock dependencies to a `pdm.lock` file. | Helps `pdm` with reproducible builds. |
| `pdm run python src/app_name/main.py` | Run the `main.py` file using the `pdm`-controller Python executable. | Prepending Python commands with `pdm run` ensures you are running them with the `pdm` Python version instead of the system's Python. You can also activate the `.venv` environment and drop `pdm run` from the beginning of the command to accomplish the same thing. |
| `pdm run custom-script` | Execute a script defined in `pyproject.toml` file named `custom-script`. | [PDM scripts](https://pdm-project.org/latest/usage/scripts/) |

## What is PDM?

Expand All @@ -51,6 +51,10 @@

PDM (Python Dependency Manager) is a tool that helps manage Python projects & dependencies. It does many things, but a simple way to describe it is that it replaces `virtualenv` and `pip`, and ensures when you install dependencies, they will be compatible with the current Python version and other dependencies you add to the project. Some dependencies require other dependencies, and where `pip` might give you an error about a `ModuleNotFound`, `pdm` is smart enough to see that there are extra dependencies and will automatically add them as it installs the dependency you asked for.

!!! note

Unlike `pip`, `pdm` uses a `pyproject.toml` file to manage your project and its dependencies. This is a flexible file where you can add metadata to your project, create [custom `pdm` scripts](https://pdm-project.org/latest/usage/scripts/), & more.

## What problem does PDM solve?

If you have already used tools like `poetry` or `conda`, you are familiar with the problem `pdm` solves. In a nutshell, dependency management in Python is painful, frustrating, and difficult. `pdm` (and other similar tools) try to solve this problem using a "dependency matrix," which ensures the dependencies you add to your project remain compatible with each other. This approach helps avoid "dependency hell," keeps your packages isolated to the project they were installed for, and skips the `ModuleNotFound` error you will see in `pip` when a dependency requires other dependencies to install.
Expand Down

0 comments on commit c34fa7d

Please sign in to comment.