Pixie: A better way to manage Python environments

by
0 comments
Pixie: A better way to manage Python environments

Pixie: A better way to manage Python environments
Image by author

, Introduction

Python is now one of the most popular languages, with applications in software development, data science, and machine learning. Its flexibility and rich 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. right here pixie Comes to the rescue. It addresses real challenges of reproducibility and portability at every stage of development. Teams working on machine learning, web applications or data pipelines get a consistent environment, smooth continuous integration/continuous deployment (CI/CD) workflows, and faster onboarding. With its isolated per-project design, it brings a modern and reliable approach to Python environment management. This article explains how to manage a Python environment using Pixie.

, Why does environmental management matter?

It may seem easier to manage a Python environment in the beginning with tools like venv Or virtualenvHowever, as the scope of projects increases, these approaches begin to show their limitations, Often, you find yourself reinstalling the same package over and over again for different projects, which becomes repetitive and inefficient, Additionally, trying to keep dependencies in sync with your teammates or production servers can be difficult; Even a small version mismatch can cause the project to fail, Sharing or replicating an environment can become increasingly disorganized, leading to situations where a setup of dependencies works on one machine but is broken on another, These environmental issues can slow down growth, create frustration, and create unnecessary inconsistencies that hinder productivity,

Pixie Workflow: From Zero to Reproducible EnvironmentPixie Workflow: From Zero to Reproducible Environment
Pixie Workflow: From Zero to Reproducible Environment Image by editor

, Step-by-Step Guide to Using Pixie

, 1. Install Pixie

For MacOS/Linux:
Open your terminal and run:

# Using curl
curl -fsSL https://pixi.sh/install.sh | sh

# Or with Homebrew (macOS only)
brew install pixi

Now, add Pixie to your path:

# If using zsh (default on macOS)
source ~/.zshrc

# If using bash
source ~/.bashrc

for Windows:
Open and run PowerShell as administrator:

powershell -ExecutionPolicy ByPass -c "irm -useb https://pixi.sh/install.ps1 | iex"

# Or using winget
winget install prefix-dev.pixi

, 2. Start your project

Create a new workspace by running the following command:

pixi init my_project
cd my_project

Output:

✔ Created /Users/kanwal/my_project/pixi.toml

pixi.toml The file is the configuration file for your project. This tells Pixie how to set up its environment.

, 3. Configure pixi.toml

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 need to edit it to include the 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)

Let’s understand the structure of the file:

  • (scope): This contains general project information, including project name, version, and supported platforms.
  • (dependencies): In this section, you specify key dependencies such as Python version.
  • (pypi-dependencies): You define Python packages to install from PyPI (e.g. numpy And pandasPixie will automatically create a virtual environment and install these packages for you. For example, numpy = "*" Installs the latest compatible version of NumPy.
  • (Work): You can define custom commands that you want to run in your project, for example, testing scripts or script execution.

, 4. Set up your environment

Run the following command:

Pixy will create a virtual environment with all the specified dependencies. You should see a confirmation like this:

✔ The default environment has been installed.

, 5. Activate the environment

You can activate the environment by running a simple command:

Once activated, all Python commands you run in this shell will use the isolated environment created by Pixy. Your terminal prompt will change to show that your workspace is active:

(my_project) kanwal@Kanwals-MacBook-Air my_project %

Inside this shell, all installed packages are available. You can also disable the environment using the following command:

, 6. Add/Update Dependencies

You can also add new packages from the command line. For example, to add SciPy, run the following command:

Pixie will update the environment and make sure all dependencies are compatible. The output will be:

✔ Added scipy >=1.16.3,<2

, 7. Run your Python script

You can also create and run your own Python scripts. Create a simple 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 like this:

This will output:

All packages loaded successfully!

, 8. Share your environment

To share your environment, commit 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

After this, you can reproduce the environment on another machine:

git clone 
cd 
pixi install

will recreate the exact same environment using pixie pixi.lock file.

, wrapping up

Pixie provides a smart approach by integrating modern dependency management with the Python ecosystem to improve reproducibility, portability, and speed. Due to its simplicity and reliability, Pixie is becoming an essential tool in the toolbox of modern Python developers. you can also check pixie documentation To know more.

Kanwal Mehreen He is a machine learning engineer and a technical writer with a deep passion for the intersection of AI with data science and medicine. He co-authored the eBook “Maximizing Productivity with ChatGPT”. As a Google Generation Scholar 2022 for APAC, she is an advocate for diversity and academic excellence. She has also been recognized as a Teradata Diversity in Tech Scholar, a Mitex GlobalLink Research Scholar, and a Harvard VCode Scholar. Kanwal is a strong advocate for change, having founded FEMCodes to empower women in STEM fields.

Related Articles

Leave a Comment