My biggest struggle while helping newcomers with Python

And my recipe for setting up Python environment on Mac.

If you are interested for the recipe only, please scroll down to the recipe section.

Struggle

That is, there are numerous of times, and this even happened to myself when I was new, that multiple Pythons that came from different sources were mingled in the machine, waiting eagerly to teach someone a hard lesson.

“Why do you have so many Python runtimes?”

Personally, I have a Mac. There are some possible sources for my Python runtimes:

System Python Python.org official installer Homebrew Anaconda/miniconda Compiled Python from source tarball Pyenv …

With all these, it’s really confusing to tell which Python my pip and pip3 is pointed to, especially for newcomers. (This is the reason I seldom do pip install <package> when I’m not in any virtual environment. Using python3 -m pip install <package> is less ambiguous. I use this to install system-wide CLI tools.) Therefore, I usually recommend Mac user to strictly only use Homebrew and forget about all the other options.

Recipe

Use Homebrew as the single source to install python. Use pipenv to manage Python and virtual environments.

In more detail:

# Use only homebrew to manage Python

$ brew install python3

# Hopefully this will fix all the confusion

$ brew link --overwrite python3 # Use pipenv to manage virtual environments for projects

$ brew install pipenv # Run brew doctor to see if there's any issue

# If any, do as what the doctor suggested

$ brew doctor

At this point, you can safely delete/ignore all other Python instances. (Except for your System Python! Just ignore it. Thanks to Tim Bell for correction.)

When you need to create a virtual environment for an isolated project, for example, a Django project, use pipenv :

$ pipenv --three

# or specify the python version

$ pipenv --python 3.6 $ pipenv install django

Conclusion

The biggest takeaway is to use single source to install (system-wide) Python. Your source could be python.org, Homebrew, Anaconda, or even tarball, but restrict yourself from mismatching them. For me, I like homebrew a lot. For some, Pyenv is the way to go because it’s easier to switch the default version. Maybe you like Anaconda the best and that’s no big deal.

What’s your biggest Python struggle? Please comment below. I’d love to hear yours!