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
Getting Started
Working with Your Project
Write Python Code
Add a function tosrc/my_python_project/__init__.py:
rich dependency:
Run Your Code
Run your Python code usingpixi run:
Install the Environment
While Pixi automatically installs dependencies when running commands, you can manually install:.pixi directory containing your isolated environment:
Managing Environments
View Installed Packages
List all packages in your environment:Check Dependency Tree
See why a package is installed: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:Working with Tasks
Create Development Tasks
Define custom tasks in yourpyproject.toml:
Python Version Management
Specify Python Version
Therequires-python field automatically manages the Python interpreter:
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 attests/test_my_project.py:
Complete Example
Here’s a completepyproject.toml for a data science project:
Best Practices
Next Steps
- Learn about using pyproject.toml in depth
- Set up multiple environments for different use cases
- Explore global tools for system-wide installations
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:- Prefer conda packages when available
- Use solve groups to ensure consistency
- Check
pixi listto see which source each package comes from
Python Version Issues
Ensure yourrequires-python field matches your dependencies’ requirements: