Skip to main content

Python

Python by default will attempt to install your packages globally, or in the Nix Store (which it does not have permissions to modify). To use Python with Devbox, we recommend setting up a Virtual Environment using pipenv or Poetry (see below).

Example Repo

Adding Python to your Project

devbox add python310, or in your devbox.json add:

  "packages": [
"python310"
],

This will install Python 3.10 in your shell.

Other versions available include:

  • python37 (Python 3.7)
  • python38 (Python 3.8)
  • python39 (Python 3.9)
  • python311 (Python 3.11)

Installing Packages with Pip

Example Repo

Open In Devbox.sh

pip is the standard package manager for Python. Since it installs python packages globally, we strongly recommend using a virtual environment.

You can install pip by running devbox add python3xxPackages.pip, where 3xx is the version of Python you want to install. This will also install the pip plugin for Devbox, which automatically creates a virtual environment for installing your packages locally

Your virtual environment is created in the .devbox/virtenv/pip directory by default, and can be activated by running source $VENV_DIR/bin/activate in your devbox shell. You can activate the virtual environment automatically using the init_hook of your devbox.json:

{
"packages": [
"python310",
"python310Packages.pip"
],
"shell": {
"init_hook": ". $VENV_DIR/bin/activate"
}
}

Pipenv

Example Repo

Open In Devbox.sh

pipenv is a tool that will automatically set up a virtual environment for installing your PyPi packages.

You can install pipenv by adding it to the packages in your devbox.json. You can then manage your packages and virtual environment via a Pipfile

{
"packages": [
"python310",
"pipenv"
],
"shell": {
"init_hook": "pipenv shell"
}
}

This init_hook will automatically start your virtualenv when you run devbox shell.

Poetry

Example Link

Open In Devbox.sh

Poetry is a packaging and dependency manager for Python that helps you manage your Python packages, and can automatically create a virtual environment for your project.

You can install Poetry by adding it to the packages in your devbox.json. You can then manage your packages and virtual environment via a pyproject.toml

{
"packages": [
"python3",
"poetry"
],
"shell": {
"init_hook": "poetry shell"
}
}

This init_hook will automatically start Poetry's virtualenv when you run devbox shell, and provide you with access to all your packages