uv/How To/Add a Package to pyproject.toml
Properties
How To Add a Package to pyproject.toml with uv
Overview
Prerequisites
- uv installed on your system
- An existing Python project with a pyproject.toml file
- Project directory structure initialized with
uv initor containing a valid pyproject.toml
Steps
1. Navigate to Your Project Directory
2. Add a Package Using the uv Command
3. Specify a Version Constraint (Optional)
4. Add Multiple Packages (Optional)
5. Add Development Dependencies (Optional)
6. Verify the Package Was Added
- Check your pyproject.toml file to confirm the dependency was added
- The package should appear in the
dependencieslist under the[project]section - Example of updated pyproject.toml:toml
[project] name = "your-project" version = "0.1.0" description = "Your project description" dependencies = [ "ruff>=0.5.0", ]
Using uv with #mise
Overview
- mise is a dev tool manager that can manage Python versions and tools like uv
- Using mise with uv provides a seamless way to manage both Python versions and packages
Setting Up mise with uv
1. Install mise
2. Configure Python with mise
3. Install uv with mise
4. Create a Task for Adding Packages
- Add a task to your
.mise.tomlfor adding packages:toml[tasks.add-pkg] description = "Add a package to pyproject.toml" run = "uv add $@" - Use it with:
mise run add-pkg ruff My Notes on this technique
- Add a task to your
Benefits of Using mise with uv
Local Configuration
Troubleshooting
- If you see
Error: No pyproject.toml found: Make sure you're in the correct directory or initialize a project withuv init - If you see
Error: Package not found: Check the package name for typos or verify it exists on PyPI - If installation fails with version conflicts: Try using a different version constraint or check compatibility with your other dependencies
- If the virtual environment isn't created: Run
uv venvto manually create it - If using mise and commands aren't found: Ensure mise is activated with
eval "$(mise activate)" - If mise tasks aren't working: Check your
.mise.tomlsyntax and try running withMISE_DEBUG=1
- If you see
Related