My Simple Python Development Setup
Keep it simple and keep your environments isolated
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
pyenv. Even though I always try to use the latest Python release, sometimes you need to work on an older codebase.
pyenv 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
pyenv 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
pyenv is that it builds each Python version you install from the source and they are all located in your
pyenv root directory:
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.
What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?
virtualenv is a very popular tool that creates isolated Python environments for Python libraries. If you're not…
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
PATH 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
venv 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
virtualenv is the way to go. Actually, the implementation of
venv is heavily based in
virtualenv so the way they work is really similar.
One main difference is that unlike
virtualenv 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
virtualenv is really similar to what we previously saw using
venv. 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
- 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
venv, it comes in the standard library.
- To manage virtual environments for older Python versions, use
virtualenv, it needs to be installed using