Skip to main content
Learn how to create Python projects with Pixi, manage dependencies from both conda and PyPI, and leverage the power of reproducible environments.

Why Use Pixi for Python Development?

Pixi builds upon the conda ecosystem, which allows you to create Python environments with all the dependencies you need. This is especially useful when working with:
  • Multiple Python interpreters
  • Bindings to C and C++ libraries
  • Packages that require system-level dependencies
  • Mixed conda and PyPI packages
For example, GDAL from PyPI doesn’t include binary C dependencies, but the conda package does. On the other hand, some packages are only available through PyPI. Pixi gives you the best of both worlds.

Getting Started

Working with Your Project

Write Python Code

Add a function to src/my_python_project/__init__.py:
First, add the rich dependency:

Run Your Code

Run your Python code using pixi run:
Expected Output:
The first run might be slow as Pixi installs dependencies, but subsequent runs will be nearly instant.

Install the Environment

While Pixi automatically installs dependencies when running commands, you can manually install:
This creates a .pixi directory containing your isolated environment:

Managing Environments

View Installed Packages

List all packages in your environment:
Example Output:
To see only explicitly installed packages:

Check Dependency Tree

See why a package is installed:
Or see what depends on a package:

Mixing Conda and PyPI Packages

One of Pixi’s powerful features is seamlessly mixing conda and PyPI packages. PyPI packages can depend on conda packages, and Pixi will resolve everything correctly.

Replace PyPI with Conda

If a dependency is installed from PyPI, you can replace it with a conda version:
Your code continues to work without any changes!

Working with Tasks

Create Development Tasks

Define custom tasks in your pyproject.toml:
Run tasks with:

Python Version Management

Specify Python Version

The requires-python field automatically manages the Python interpreter:
Pixi automatically installs the appropriate Python version - no more brew, apt, or system installation steps!

Use Free-threaded Python

For free-threaded Python (PEP 703), add:
Free-threaded Python is experimental and may not work with all packages yet.

Testing Your Code

Create a test file at tests/test_my_project.py:
Add pytest and create a test environment:
Run tests:
Expected Output:

Complete Example

Here’s a complete pyproject.toml for a data science project:

Best Practices

Use the right package source:
  • Use conda for packages with system dependencies (numpy, scipy, opencv)
  • Use PyPI for pure Python packages
  • Pixi can mix both seamlessly
Leverage editable installs: The editable install means you can modify your code and immediately see changes without reinstalling.
Create multiple environments: Separate development, testing, and production dependencies using features and environments.

Next Steps

Troubleshooting

Slow Installation

The first installation downloads and installs all dependencies. Subsequent installations are much faster due to caching.

Package Conflicts

If you encounter conflicts between conda and PyPI packages, try:
  1. Prefer conda packages when available
  2. Use solve groups to ensure consistency
  3. Check pixi list to see which source each package comes from

Python Version Issues

Ensure your requires-python field matches your dependencies’ requirements: