Managing Multiple Python Installs and Packages with Pyenv and Pipenv

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//.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users//.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

I typically also run the following commands to not have any analytics forwarded:

export HOMEBREW_NO_ANALYTICS=1
brew analytics off

Note, if you already have Homebrew installed, make sure to update the Homebrew repositories and packages with the below commands:

brew update
brew upgrade

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.

Helpful Commands

Python:
Check to see which python versions is being used (by default macOS uses the system Python version).

python --version
or
python3 --version (latest Macs only have python3 installed by default)

Pyenv

To see all python versions available to install:
pyenv install --list

To install a specific python version:
pyenv install (Ex: pyenv install 3.11.1)

To set the python version to use by default:
pyenv global (Ex: pyenv global 3.11.1)

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.

pyenv install (Ex: pyenv install 3.9.10)
cd (Ex: cd MyProject)
pyenv local (Ex: pyenv local 3.9.10)

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:

pyenv versions

Pipenv

Create a virtual environment:

cd (Ex: cd MyProject)
pipenv install

Installing a package:

cd (Ex: cd MyProject)
pipenv install (Ex: pipenv install requests)

Updating a package:

cd (Ex: cd MyProject)
pipenv update (Ex: pipenv update requests)

Uninstalling a package:

cd (Ex: cd MyProject)
pipenv uninstall (Ex: pipenv install requests)

Running code:

cd (Ex: cd MyProject)
pipenv run python (Ex: pipenv run python my_code.py)

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

This entry was posted in Development Tools, Mac, Mac OS Ventura, Mac OS X Big Sur, Mac OS X Catalina, Mac OS X Mavericks, Mac OS X Mojave, Mac OS X Monterey, Mac OS X Yosemite, Operating Systems, Programming Languages, Python, Technology, Tips & Tricks and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Managing Multiple Python Installs and Packages with Pyenv and Pipenv

  1. Pingback: asdf for Managing Multiple Tool Runtime Versions | HumairAhmed.com

  2. Bryan S says:

    Excellent post. Thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *


five − = 3