Python HOW: Farewell Anaconda! Take Full Control of Your Development Environment

Setting up python with pyenv, venv, pipx, and vsCode on Windows and macOS

Image for post
Image for post
Photo by Kevin Ku from Pexels

Anaconda and Miniconda are amazing python distributions that get you up and running out-of-the-box. Once you start deploying your projects into production, however, you will defiantly need more control. In this article, I go through some of the best tools to make that transition happens! 🤘

All the CLI commands used in this article are in for Windows, and for macOS

Before starting up 🚿

If you’ve anaconda or miniconda installed, uninstall it as detailed here. For Windows, also:

  • Delete if you have previously ran
  • Disable the built-in python launcher: search for Manage app execution aliases ▶️ disable App Installer aliases for python

Install python 🐍

Download the latest 64-bit python Release for Windows or macOS ( at the time of writing), and install it (💀without adding python to 💀). The installation directory is:

To use, specify the full path, e.g: ‘$HOME\AppData\Local\Programs\Python\Python39\python.exe’

⚠️ Because we haven’t added python to the system’s , each time we want to use it, we have to give the full path. We can indeed do that. However, different projects require different versions of python, which makes this impractical. Solution? 👇

pyenv: python version management tool 🐍🐍

pyenv lets you easily install and switch between multiple versions of python globally and locally” by using executables (more on here)

To understand how works, let’s first install it:

Windows: install with in the home directory, add an environment variable , and add & directories to the system’s , and rehash :

Full installation instructions here. Installation directory is ‘$HOME\.pyenv’

MacOS: install with , add to shell ( adds and to , installs autocompletion, and rehash ), and install some recommended python build dependencies:

Full instructions here. Installation directory is ‘/usr/local/Cellar/pyenv’

Now we’ve installed, close and reopen your CLI, then let’s use it to install python versions and :

The same applis for zsh. Full usage commands here

installs python versions in the following directory:

To make version global for example (i.e. register it with through ), we use the command:

For zsh use ‘which’ instead of ‘where.exe’ in line 9

Great! now we can make any version of python global 🎉 (we didn’t talk about making it local but you can check the command)

⚠️ However, not only that each project requires a different versions of python, but it also requires for that version to be isolated in a virtual environment with all the required packages for the project to run. This allows us to take a snapshot of all the project’s requirements for reproducibility and for deployment. Solution? 👇

venv 📩

“The venv module provides support for creating lightweight virtual environments with their own site directories”. The module was introduced in python 3.3, and as of then, it’s included with any installation of python

We can easily create an isolated virtual environment using the command. The key point is: the created environment will have its own python binary 🐍 which matches the version of the binary that was used to create it

Here, for example, the virtual environment we create will also have python :

The same applies for zsh

Before installing any dependencies for that project, however, we need to activate the virtual environment. Luckily, activation scripts for , , and are also created for us in the sub-folder 💃

For zsh, use source ‘.venv/bin/activate’ in line 2

To take a snapshot of your project dependencies, use the command in :

For zsh use ‘cat’ instead of ‘Get-Content’ in line 5

If you already have the project’s , go through the same steps but instead of installing the dependencies as before, do: to install them from the requirements file

Note: to delete the virtual environment, simply delete folder

⚠️ There’re few tools that you should be using for any project, for example, code formatters and linters, that shouldn’t be included as a dependency,. It wouldn’t make sense to install them for each project then delete them from (and their own dependencies!). Solution? 👇

pipx

pipx focuses on installing and managing Python packages that can be run from the CLI directly as applications”. does that by creating an isolated environment for that package (and its dependencies), that we can then accesses from the CLI globally for any project 🙌

To understand how works, let’s first install it (full instructions here):

Windows: install with from the main python installation (i.e. ), and run to add directories to the system’s :

Installation directory is ‘$Home\AppData\Roaming\Python\Python39\site-packages’

MacOS: install with , and run to add directories to the system’s :

Installation directory is ‘/usr/local/Cellar/pipx’

The directories that actually adds to are its binary directory (so we can run it from the CLI), and the directory where packages’ binaries would go (so we can run them from the CLI), these are:

Directories in lines 2&6 have the pipx binary. Directories in lines 3&7 will have installed packages binaries

Now we’ve installed, close and reopen your CLI, then let’s use it to install the following helper packages:

  • code formatter with “a strict subset of PEP 8 coding style”
  • for sorting imports alphabetically, and automatically separated into sections and by type
  • code linter for style guide enforcement
The same applies for zsh. Full usage commands are here

Each package will be installed in an isolated virtual environment inside , and have its binary in (which is already in thanks to ). To use any of these applications, we can simply run it in the CLI:

The same applies for zsh

⚠️ If you’re using vsCode, you might be wondering “how to tell vsCode where to find the global packages installed with so I can use them with any project? Solution? 👇

Setting up vsCode

Settings for pipx packages

You can tell vsCode to use all the helper packages we installed using , and more importantly, where to find the binaries 📦 in the settings

vsCode saves settings in a file, to bring the Palette, then search for :

  • Define the formatting provider as and provide its binary path (more on formatting here). As defaults to characters per line, we use the same length for the editor’s ruler (more on line length here)
Replace $HOME with your HOME path (vsCode doesn’t define $HOME). For macOS, use / instead of
  • Define the imports sorting path for (vsCode uses as an import sorting provider by default as you can see here). “ also formats imports, but in a different way from defaults which leads to conflicting changes” 😵. To fix this, we can pass few arguments to to make it consistent with (more details here)
Replace $HOME with your HOME path. For macOS, use / instead of
  • Enable for linting and provide its binary path (more on linting here). “There are a few deviations that cause incompatibilities with . To fix this, we can pass few arguments to make consistent with (more details here)
Replace $HOME with your HOME path. For macOS, use / instead of

Settings Sync

You can sync your settings, keybindings, and installed extensions across your machines by following the instructions here (you need a Microsoft or a Git account). However, all the binaries paths for the packages we installed using shouldn’t be synced between Windows and macOS. Luckily, we can ignore these by adding them to :

Note: you can add any settings in to the list of ignored settings above

What is next?

  • Start using Docker 🐋 for Python
  • Become a Scikit-learn plumber 🚿

Happy coding!

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Gabriel Harris Ph.D.

Written by

I’m an End-to-End data scientist and a Python educator. Most of my articles start after saying “I wish someone has written about this!”, maybe I should?

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Gabriel Harris Ph.D.

Written by

I’m an End-to-End data scientist and a Python educator. Most of my articles start after saying “I wish someone has written about this!”, maybe I should?

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store