Set up Python environment correctly on Mac

Rain Wu
Random Life Journal
3 min readMay 9, 2020

Yesterday I got my new notebook and start migrating from the old one, it means I have a chance to setting up developing environment in a brand new device. It’s an important issue that affects the future development experience, I will share my approach in this article and hope it can help you.

Install brew

brew is the popular package manager for MacOS. In fact, I didn’t used brew before, but most of my friend rate it well, so I think I should give it a try. You can install it via the instructions on thier website or just execute the command below.

$ /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Clean Environment

Mac has a pre-install python2.7 environment, which includes package management tools. In order to avoiding ambiguous while using pip or pip3, remove the build-in tools first.

$ sudo pip uninstall pip
$ sudo pip3 uninstall pip

pyenv

pyenv is a python version management tool that make us available to handle multiple version of python in the same device, and manage them through a unified user interface.

$ brew install pyenv

Check the version available currently, and install the version you use. I will install python3.8.2 first and set it as global default.

$ pyenv install --list
$ pyenv install 3.8.2
$ pyenv global 3.8.2

Let pyenv can associate with your shell by append the scripts into config file, it’s the key step for pyenv to make it works. Use source command to make him effective immediately.

$ echo -e ‘if command -v pyenv 1>/dev/null 2>&1; then\n eval “$(pyenv init -)”\nfi’ >> ~/.zshrc$ source ~/.zshrc

Check whether configuration already updated with — version flag, need to pay attention to whether the path of pip is provided by pyenv.

$ python --version
Python 3.8.2
$ pip --version
pip 19.2.3 from /Users/rainwu/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pip (python 3.8)
$ pip3 --version
pip 19.2.3 from /Users/rainwu/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pip (python 3.8)

And remember to update your pip/pip3 to latest version, or it may cause some unexpected issues during installing tools.

$ pip3 install --upgrade pip

pipenv

I used to build an independent virtual environment for each project or repository. In additions to avoiding chaos in package management, this makes me convenient in the process of containerization.

$ pip3 install pipenv
$ pipenv --version

Try to create a new virtual environment within your repository, I select python3.8.2 here.

$ cd /Repositories/HelloWorld
$ pipenv --python 3.8

Install any package for checking whether pipenv works correctly, and glance at the new Pipfile.

$ pipenv insall requests==2.23.8
$ cat Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages][packages]
requests = "==2.23.0"
[requires]
python_version = "3.8"

The python environment was deployed sucessfully now, please give me some claps if this article is helpful to you :)

--

--

Rain Wu
Random Life Journal

A software engineer specializing in distributed systems and cloud services, desire to realize various imaginations of future life through technology.