Python development setup

How to set up and configure your system for efficient Python development

Oct 20, 2021 · 2 min read

My list of steps for setting up an efficient Python development environment - no rocket science and can probably be found all over the internet, but a simple compilation for my own reference and possibly even helpful to others.

Target OS: Mac OS

Managing multiple Python versions with pyenv

Pyenv is an amazing tool that lets you install multiple Python versions and seamlessly switch between them. I find this extremely helpful when working on different Python projects that require me to use different Python versions.

Install via Homebrew:

brew update
brew install pyenv

Details about installation, configuration and usage can be found in the well structured Pyenv documentation.

Manage dependencies in Python virtual environments

A well established best practice in Python development is to create individual virtual environments for individual projects. Virtual environments are Python environments that ensure the isolation of the Python interpreter and Python packages that are installed into it. By using virtual environments, you can, for example, use conflicting versions of external libraries in different projects without conflicts.

There are two ways that I use to create and manage virtual environments and manage dependencies in them:

  • Python's built in Venv module and a simple requirements.txt file
  • Poetry, a great tool that helps with dependency management and resolution of version conflicts. It also uses venv by default to automatically create virtual environments for all projects.

Poetry

The installation is straightforward and instructions can be found in the excellent Poetry docs. No need to repeat things here.

After installation I configure poetry to create virtual environments directly in the respective projects:

poetry config virtualenvs.create true
poetry config virtualenvs.in-project true

Create a new Python project including the spawn of a new virtual environment:

poetry new the-project-name

Venv and requirements.txt

The alternate and more traditional way to manage dependencies in a Python project is using the built-in venv module and a plain text file, typically named requirements.txt.

After manually creating a new directory for your Python project create a new virtual environment and activate it:

python -m venv venv
source venv/bin/activate