I wrote a blog last year titled “Managing Multiple Python Versions and Packages.” In that blog post, I discussed how you can easily deploy and manage multiple python versions and packages without running into issues later on. Here I go into a bit more detail on setup and usage.
Specifically, I followed this process and installed the following:
- Install Pyenv (Ex: brew install pyenv)
- Using Pyenv – Install Python versions you would like to use and set one as Global; this will be your default (Ex: pyenv global 3.9.10)
- Install Pipenv (Ex: pip install –user pipenv)
First thing you want to do is install Homebrew if you don’t already have it. Check out the instructions on the Homebrew site. Basically, you can copy and post the below command into your terminal.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
You’ll then be given some commands to run in your terminal to add Homebrew to your PATH. Something like below (note, in my case it’s specific to Zsh):
echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /Users/
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/
eval "$(/opt/homebrew/bin/brew shellenv)"
I typically also run the following commands to not have any analytics forwarded:
brew analytics off
Note, if you already have Homebrew installed, make sure to update the Homebrew repositories and packages with the below commands:
Once Homebrew is installed, use it to install Pyenv, which is the tool used to manage multiple python installs.
brew install pyenv
Pyenv manages the python runtimes, but you will also want to install pipenv to manage the python packages as shown below.
pip install --user pipenv
pip install --upgrade pip
or for Python3
pip3 install --user pipenv
pip3 install --upgrade pip
Next, there is some shell configuration involved for pyenv. The example I provide is for Zsh. If you are using a different shell, checkout the pyenv Github page for instructions.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
If you wish to get pyenv in noninteractive login shells as well, also add the commands to ~/.zprofile or ~/.zlogin.
Check to see which python versions is being used (by default macOS uses the system Python version).
python3 --version (latest Macs only have python3 installed by default)
To see all python versions available to install:
pyenv install --list
To install a specific python version:
To set the python version to use by default:
Now, all this is great, but what if you need a particular python version for a specific project? That’s where you can do the following:
If a particular project needs a different python version, then install that version first (if not installed already), navigate to the project folder, and set that version as local to that folder.
With the above, the python 3.9.10 version will be used for that specific project, but all other projects will use the python 3.11.1 version set globally.
If you need to check all python versions installed, you can use the following command:
Create a virtual environment:
Installing a package:
Updating a package:
Uninstalling a package:
pipenv run python
You can also use "pipenv shell" instead of "pipenv run" to activate or spawn a new shell to ensure all commands have access to your installed packages. 'exit' command will activate the environment.
Follow me on Twitter: @Humair_Ahmed