Setting up Python on a Unix machine (without pyenv)

2017-01-19

If I don’t need access to multiple Python versions, but do want multiple
virtual environments, this is how I set up Python on a new Unix/macOS
machine. You don’t need to sudo or run any of this as root. All of it
can be easily undone. Just delete the virtual environment that ails you (or
~/.virtualenvs if you want to start completely from scratch).

Note: this post is about how to set up multiple Python environments on a
development machine. It doesn’t cover how to have multiple Python
interpreters on a single machine. I have [another post]() about that.

macOS-only prerequsite

Install homebrew and use it to install isolated, up-to-date versions of Python
and legacy Python 2.

brew install python python3

Optionally (and this is not Python related), update git and install some
goodies:

All systems

Bootstrap pip:

python -m ensurepip --user
python3 -m ensurepip --user

This will result in an error on Debian/Ubuntu systems, but won’t do any harm.

Then install/update the Python packaging
basics in your
“global” Pythons. These global Pythons will be the system versions on a Linux
system, or the homebrew versions on macOS (assuming you’ve installed homebrew
as described above):

If you prefer to put virtual environments in the same directory as the
corresponding project or application, then change the shell function above or
just create each project by hand:

cd my_project
python3 -m virtualenv venv

You can use the workon function to activate a particular environment, as long
as it lives in ~/.virtualenvs:

workon default

I have workon default in my shell startup.

A fresh install of macOS will add the name of the active virtual environment to
your bash prompt. This is not necessary, but is nice to have. If you want it
and your system doesn’t, or you have specific requirements, then there are lots
of options including, e.g. my
dotfiles,
which I pinched from
StackOverflow.

I use my default environment for quick tests and day-to-day coding not
associated with a particular project. For that work, at a minimum, I need a few
basics:

Optional extra: autoenv

Install autoenv to run a set of
commands every time you change into a directory. These commands are shell
commands contained in a file .env in that directory. I use them to ensure the
appropriate virtual environment is activated, and any necessary environment
variables are set (which I use a lot, per 12
Factor).

The my_project environment is now automatically enabled when I cd into
~/my_project.

Optional extra: pipsi

Install pipsi and use it to install
Python-based command line tools so they are accessible from any environment and
you don’t have to install them in every one. First add ~/.local/bin to your
path then