Setting up your local Python environment on MacOS

As you probably know Python is an extremely useful and versatile programming language. However, it is confusing when it comes to setting up a proper local environment for developing in Python.

Defining what is proper

A proper local environment should have the following characteristics:

  • It should not interfere with your Operating System
  • It should be easy to replicate
  • It should favour automation
  • It should be secure by design

With those principles in mind, we can start creating our local environment.

A virtual environment

Virtual environments are extremely popular since they allow us to have a playground isolated from our host operating system. We can define its dependencies and install the software in the virtual environment instead of the host machine.

A virtual machine would satisfy our first criteria for not interfering with our operating system.

An Introduction to Pipenv

Pipenv is a package manager that enables us to create virtual environments in our projects. If you are coming from the JavaScript world it is extremely similar to npm and provides us with a Pipfile that mimics the package.json found in any Node application.

An example of a Pipfile can be found below:

[[source]]
name = "pypi"
url = "<https://pypi.org/simple>"
verify_ssl = true[dev-packages]
pylint = "*"[packages]
webbrowser = "*"
twine = "*"[requires]
python_version = "3.7"

The Pipfile specifies for each project which Python version it requires as well as development and production dependencies with a version (if specified).

The Pipfile meets our second criteria of being able to reproduce easily.

To install Pipenv you can use brew if you are on macOS.

brew install pipenv

After installing Pipenv, I also recommend setting the following environment variable in your .bash_profile or .zshrc.

export PIPENV_VENV_IN_PROJECT=1

The command above tells Pipenv to install the virtual environment in your project directory instead of using a global directory. This allows us to contain all requirements to a single directory.

Starting your development environment

Once you have created a working directory for your project and installed pipenv you can go ahead and type the following command pipenv install.

The command above will create a virtual environment in your local working directory.

drwxr-xr-x .venv <--- Virtual Environment
-rw-r--r-- Pipfile
-rw-r--r-- main.py

If you need to install dependencies, rather than using pip which would point to the host machine's package manager, you are going to use pipenv install <package>. This command will install the dependency in the virtual machine and add it to the Pipfile with the appropriate version (if specified).

Finally, we need to activate our virtual Python installation.

An easy way to check if we are using our virtual Python installation is with the following command.

which python3

By default it should return the host machine's location of Python, if you are using macOS, you will probably see the following output.

/usr/local/bin/python3

Let us activate the local Python installation with the following command:

pipenv shell

Now, let's check again which Python version we are using with which python3.

The output should now reflect the local working directory of your project rather than the path to your host machine.

/src/test/python-test/testing-package/.venv

Congratulations! You have a working local Python development environment.

In the following blog post, we will talk on how to publish your Python package to pip and manage different versions.