My Simple Python Development Setup

Keep it simple and keep your environments isolated

Agustin Castro
Nov 15 · 4 min read
Photo by Max Nelson on Unsplash

I love Python. My first real development job was developing Django web applications and I always loved the expressiveness of the language and how it’s a decent tool for the job in almost all areas of software development that I’m interested in.

This is a brief explanation of the environment I use to develop Python applications and how to set it up.


What Are My Setup Requirements?

  • Keep the OS default Python installation untouched and pristine.
  • Manage multiple isolated Python versions.
  • Manage virtual environments to isolate project requirements.

Keep Your OS Default Python Installation Untouched

I like to keep my operating system as clean as possible, everything runs more smoothly this way. When developing and especially when testing and learning new things, we all make mistakes and break things.

Isolate your development environment, so when you inevitably break something, the damage is also isolated.

The Python version of your operating system belongs to your OS. It’s usually not the Python version you want, in the case of macOS it’s Python 2.7, and by installing packages globally, you’ll sooner or later have problems when needing multiple versions of the same package.

The other reason why you should leave your operating system’s Python alone is that some processes of your operating system might use that Python version. If you change it, you could damage your operating system.


Manage Multiple Isolated Versions of Python

For managing multiple Python versions I use . Even though I always try to use the latest Python release, sometimes you need to work on an older codebase. helps you easily switch the Python version.

As I use macOS, I tend to install almost all the development tools I need using Homebrew.

brew install pyenv

Usually, will be added to your path after installation. To install any Python version, do the following:

# Show the list of available python versions# If you don’t see the python version you may try updating pyenv to its latest versionpyenv install — list# Install the version you wantpyenv install -v 3.7.2# List your installed python versions (The * will indicate your current version)pyenv versions# Change your global python versionpyenv global 3.6.8# Sets a location specific python version (creates a .python-version file)pyenv local 2.7.15# Uninstall any versionpyenv uninstall 2.7.15

One advantage of is that it builds each Python version you install from the source and they are all located in your root directory:

~/.pyenv/versions/

Manage Virtual Environments

The way you keep your project dependencies isolated and well-managed when developing in Python are virtual environments.

There are a lot of different tools to do this, if you want a more in-depth comparison between the most common tools check out this great Stack Overflow answer.

Both of the tools we are going to discuss work similarly. They create a folder for your Python installation and dependencies and they modify the environment variable to point it to the desired installation.

As I try to use the latest Python 3.x version for new projects, I use to manage virtual environments. It’s included in Python’s standard library since version 3.3, this means you don’t have to install any extra tools.

Imagine you want to start a new project using Python 3, you create a new folder for your project and run:

# Create our virtual environment named “env”python3 -m venv env# A python installation in a new env folder will be created.# Activate your virtual envsource env/bin/activate# You’ll be working in your virtual environment…. Happy hacking!# To leave your virtual environmentdeactivate

If you need to support Python 2 or versions below 3.3 then is the way to go. Actually, the implementation of is heavily based in so the way they work is really similar.

One main difference is that unlike , is not a part of Python’s standard library. You need to install it using :

pip install virtualenv

The way you create virtual environments with is really similar to what we previously saw using . Navigate to your project folder and run:

# Create our virtual environment named “projectenv”virtualenv projectenv# A python installation in a new projectenv folder will be created.#Activate your virtual envsource env/bin/activate# You’ll be working in your virtual environment…. Again, happy hacking!# To leave your virtual environmentdeactivate

Recap

  • Don’t mess up your OS Python installation, you’ll regret it.
  • Manage multiple Python versions with .
  • Avoid installing project-specific packages globally, the solution is to use virtual environments.
  • To manage virtual environments for Python versions >= 3.3 use , it comes in the standard library.
  • To manage virtual environments for older Python versions, use , it needs to be installed using .

Better Programming

Advice for programmers.

Agustin Castro

Written by

I’m a software engineer at Globant, always interested in learning new technologies. You can contact me here www.linkedin.com/in/agustincastro91

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade