
Photo by the author
# Entry
Python is currently one of the most popular languages used in software development, data analysis and machine learning. Its flexibility and opulent collection of libraries make it a favorite among developers in almost every field. However, working with multiple Python environments can still be a significant challenge. This is where Pixi comes to the rescue. It addresses the real challenges of reproducibility and portability at every level of development. Teams working on machine learning, web applications, or data pipelines gain consistent experiences, smoother continuous integration/continuous deployment (CI/CD) workflows, and faster deployments. With an isolated design for individual projects, it provides a contemporary and reliable approach to managing your Python environment. This article describes how to manage Python environments with Pixi.
# Why environmental management matters
Managing Python environments may seem basic at first with tools like venv Or virtualenv. However, as the scope of projects increases, these approaches reveal their limitations. It’s common to find yourself installing the same packages multiple times for different projects, which becomes repetitive and ineffective. Additionally, trying to synchronize dependencies with team members or between production servers can be tough; even a minor version mismatch can cause a project to fail. Sharing or replication environments can quickly become disorganized, leading to situations where one dependency configuration works on one machine but breaks on another. These environmental issues can snail-paced down development, cause frustration, and introduce unnecessary inconsistencies that hamper productivity.


Pixi Workflow: From Zero to Reproducible | Photo by the editor
# Step-by-step guide to using Pixi
// 1. Install Pixi
For macOS/Linux:
Open a terminal and run:
# Using curl
curl -fsSL https://pixi.sh/install.sh | sh
# Or with Homebrew (macOS only)
brew install pixi
Now add Pixi to your PATH:
# If using zsh (default on macOS)
source ~/.zshrc
# If using bash
source ~/.bashrc
For Windows:
Open PowerShell as administrator and run:
powershell -ExecutionPolicy ByPass -c "irm -useb https://pixi.sh/install.ps1 | iex"
# Or using winget
winget install prefix-dev.pixi
// 2. Initialize your project
Create a recent workspace by running the following command:
pixi init my_project
cd my_project
Exit:
✔ Created /Users/kanwal/my_project/pixi.toml
The pixi.toml file is your project’s configuration file. Tells Pixi how to set up the environment.
// 3. Configure the pixi.toml file
Currently yours pixi.toml looks something like this:
[workspace]
channels = ["conda-forge"]
name = "my_project"
platforms = ["osx-arm64"]
version = "0.1.0"
[tasks]
[dependencies]
You’ll need to edit it to include your Python version and PyPI dependencies:
[workspace]
name = "my_project"
channels = ["conda-forge"]
platforms = ["osx-arm64"]
version = "0.1.0"
[dependencies]
python = ">=3.12"
[pypi-dependencies]
numpy = "*"
pandas = "*"
matplotlib = "*"
[tasks]
We understand the file structure:
- [workspace]: : Provides general information about the project, including the project name, version, and supported platforms.
- [dependencies]: : In this section you specify basic dependencies such as the Python version.
- [pypi-dependencies]: : You define Python packages to install from PyPI (e.g
numpyANDpandas). Pixi will automatically create a virtual environment and install these packages for you. For example,numpy = "*"installs the latest compatible version of NumPy. - [tasks]: : You can define your own commands that you want to run in your project, such as testing scripts or executing scripts.
// 4. Install your environment
Run the following command:
Pixi will create a virtual environment with all the specified dependencies. You should see a confirmation like:
✔ The default environment has been installed.
// 5. Activate the environment
You can activate the environment by issuing a elementary command:
Once activated, all Python commands run in this shell will operate the isolated environment created by Pixi. The terminal message will change to indicate that the workspace is dynamic:
(my_project) kanwal@Kanwals-MacBook-Air my_project %
All installed packages are available inside this shell. You can also deactivate the environment with the following command:
// 6. Add/update dependencies
You can also add recent packages from the command line. For example, to add SciPy, run the following command:
Pixi will update the environment and make sure all dependencies are compatible. The output will be as follows:
✔ Added scipy >=1.16.3,<2
// 7. Run your Python scripts
You can also create and run your own Python scripts. Create a elementary Python script, my_script.py: :
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy
print("All packages loaded successfully!")
You can run it as follows:
This will display:
All packages loaded successfully!
// 8. Share your environment
To share your environment, please approve it first pixi.toml AND pixi.lock for version control:
git add pixi.toml pixi.lock
git commit -m "Add Pixi project configuration and lock file"
git push
You can then recreate the environment on another computer:
git clone
cd
pixi install
Pixi will recreate the exact same environment using the file pixi.lock file.
# Summary
Pixi provides an wise approach by integrating contemporary dependency management with the Python ecosystem to improve reproducibility, portability, and speed. Due to its simplicity and reliability, Pixi is becoming an crucial tool in the toolkit of contemporary Python developers. You can also check Pixi documentation to find out more.
Kanwal Mehreen is a machine learning engineer and technical writer with a deep passion for data science and the intersection of artificial intelligence and medicine. She is co-author of the e-book "Maximizing Productivity with ChatGPT". As a 2022 Google Generation Scholar for APAC, she promotes diversity and academic excellence. She is also recognized as a Teradata Diversity in Tech Scholar, a Mitacs Globalink Research Scholar, and a Harvard WeCode Scholar. Kanwal is a staunch advocate for change and founded FEMCodes to empower women in STEM fields.
