Python is one of the most popular programming languages. Just search "top programming languages of 2020." And then, try searching "how to set up Python." There are many, many ways to set up Python, and it can depend on some of the following: the host machine’s OS, the host’s package manager of choice, the choice of Python package manager, whether to install Python to the host machine or a virtual machine or even Linux container, and how install pathing is set up on the host machine.
The following tips are not exhaustive, but they have certainly helped me navigate setting up my Python development environment on a Macbook.
This first tip is helpful for developers of any programming language working on a Mac. Install and use Homebrew. It really is the missing package manager for Macs. If Ubuntu users can enjoy apt-get, Mac users can enjoy brew. Supposedly, it works on Linux too, but I have only used it on Macs.
Run the below command to get it installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
A couple of helpful packages I always install are "htop" for monitoring CPU usage and "bash-completion" to reduce typing. You can also use it to install Python, but we will get to Python installs later.
Although Python can be installed directly to the host machine, it is beneficial at times to install and set up a Python in a more controlled environment. Vagrant and Docker both offer solutions to isolating the Python developer environment. Vagrant offers a declarative configuration approach to setting up a virtual machine. Docker also follows this pattern, except it sets up and runs a Linux container instead of an entire virtual machine.
An entire blog post series can be devoted to either technology. Just know that they are out there, and if you are setting up an existing codebase, you may run into one or both of these platforms.
We went over how to install Python to the host machine with tools like Homebrew. But we may want to have multiple versions of Python installed--and do so without setting up a whole separate container or VM to do it.
Pyenv utilizes a powerful native Python tool, the virtual environment. The virtual environment (i.e., venv) sets up a specific version of Python and other packages in an isolated directory on the file system.
Python uses a manager called pip to install various external packages.
You may have used it already to set up your app by running
pip install -r requirements.txt
or pip install -e .
in your local repo.
Oftentimes, an organization will use its own remote repository of Python packages rather than a public one. There are security and vetting advantages to maintaining one's own Python packages. When installing Python packages into a locally or remotely hosted app, we'll want to include the "index-url" parameter in our call to "pip install." Instead of using the default value, https://pypi.org/simple, we’ll want to pass in the URL of the private Python package index--every time we call "pip install."
Pip offers a method to avoid having to pass the index URL every time. When running “pip,” it checks for a configuration file--the pip.conf. The default path for the config file depends on the OS of the machine; for instance, on Unix systems, it lives in /etc/pip.conf. It takes the format of an INI file, and a common approach on a work computer is to set up a block under [global]. An example would be the following:
[global]
index-url =
extra-index-url = https://pypi.python.org/pypi
trusted-hosts = python.org
This way, we can run any number of “pip install” commands, and pip will use these fields defined in the pip.conf to fill in the gaps.
Coding in Python can and should be fun. As developers, we have the ability to map ideas to functional applications that can help you or others. And with a proper Python development setup, anyone can start happily coding the next great idea.