Python Environment Setup
Choosing between Docker and native Python
Section titled “Choosing between Docker and native Python”You can either run Python in a Docker container or natively on your development machine, through various different options.
Docker provides a more consistent way to package your application, however it is slower, takes more resources, and can be more complex to integrate with IDEs, debuggers, and other development tools.
Native Python can be slightly more difficult to set up (primarily on Windows), but once it is working it is typically easier to work with.
Using Docker or uv is recommended. You can try either of these and switch if you run into problems.
Using Docker
Section titled “Using Docker”See the Docker documentation to set up your development environment with Docker.
Docker environments support using uv or pip-tools as a package manager. Uv is recommended, and pip-tools is only for legacy projects. For help adding and removing Python packages after setup, see the documentation for uv or pip-tools (deprecated).
Using uv
Section titled “Using uv”It’s recommended that new projects not using docker use uv to manage their Python environments. It is faster and simpler to use than other alternatives, and can even install and set up Python for you.
Note: uv support is only available from Pegasus version 2024.12 onwards. To use uv you must select it under the “Python package manager” setting in your project configuration.
To set up Python with uv, first install uv. Pegasus requires uv version 0.5 or higher.
On Linux / Mac:
curl -LsSf https://astral.sh/uv/install.sh | shOn Windows:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"After installing uv, go into your project directory and run:
uv syncThis should:
- Install the right version of Python (if necessary).
- Create a new virtual environment in a
.venvfolder inside your project. - Install all the project dependencies.
To see if it worked, run:
uv run manage.py shellIf you get a Python shell that looks something like this, it worked!
$ uv run manage.py shellPython 3.12.6 (main, Sep 9 2024, 22:11:19) [Clang 18.1.8 ] on linuxType "help", "copyright", "credits" or "license" for more information.(InteractiveConsole)>>>You should be able to use uv run to run any Python command on your project, or you can run:
source .venv/bin/activatein your project root to use python and other commands normally.
Using Native / System Python (with Virtual Environments and pip-tools)
Section titled “Using Native / System Python (with Virtual Environments and pip-tools)”The following are other options---which are typically recommended for developers who are already familiar with Python and one of these choices.
Unlike docker and uv, most of these require having Python installed on your machine,
so if you haven’t already, first install Python version 3.12+:
- On Mac and windows you can download Python 3.12 installers from here.
- On Ubuntu it’s recommended to use the deadsnakes repo.
Note: running on older Python versions may work, but 3.12 is what’s tested and supported.
After installing Python, set up your virtual environment through one of the following methods:
Using your IDE
Section titled “Using your IDE”Many IDEs will manage your environments for you. This is a great and simple option that you won’t have to fiddle with. Check your specific IDE’s docs for guidance on how to do this.
Be sure to choose Python 3.12 when setting up your virtual environment. If you don’t see 3.12 as an option, you may need to install it first.
Using venv
Section titled “Using venv”The easiest way to set up a virtual environment manually is to use Python’s built in
venv tool:
python3.12 -m venv /path/to/environmentIn the command below, you should replace python3 with the Python version you are using, and
/path/to/environment/ with the location on your system where you want to store the environment.
This location can be somewhere in your project directory (.venv and venv are common choices)
or anywhere else on your system.
/home/<user>/.virtualenvs/<project> is a common choice that works well with virtualenvwrapper (see below).
To activate/use the environment run:
source /path/to/environment/bin/activateYou will need to activate this environment every time you work on your project.
Using virtualenv
Section titled “Using virtualenv”virtualenv is an alternate option to venv.
On later versions of Python there’s no real reason to use it, but if you’re familiar with it
you can keep using it without any issues.
First make sure it’s installed
and then run the following command:
virtualenv -p python3.12 /path/to/environmentLike above, you should replace the python3.12 variable with the version you want to use,
and the /path/to/environment with wherever you want to set up the environment.
Like with venv, to activate the environment run:
source /path/to/environment/bin/activateAnd, like venv, you will need to activate this environment every time you work on your project.
Using virtualenvwrapper
Section titled “Using virtualenvwrapper”Virtualenvwrapper is an optional convenience
tool that helps manage virtual environments.
You can use it with either venv or virtualenv above.
If you choose to use virtualenvwrapper you can use the following command to create your environment.
This can be run from anywhere since virtualenvwrapper manages the location of your envs for you
(usually in /home/<user>/.virtualenvs/).
mkvirtualenv -p python3.12 {{ project_name }}Then to activate the environment you use:
workon {{ project_name }}Note: You can use virtualenvwrapper no matter how you created the environment.
It provides a nice set of helper tools, but can be a bit finicky to set up.