Installing python via Pyenv on Mac

preface

Pyenv is a python version management tool. Using it, you can install multiple Python versions in the system and uninstall or switch versions at any time.
You can use Homebrew (recommended) or git to install Pyenv.
The code in the black box in the article needs to be input in the terminal of Mac system.

1 install Pyenv

1.1A installing with Homebrew

1.1A.1. Install Homebrew

Reference link: Homebrew official website

Enter the following command in the terminal to install Homebrew
You need to enter the administrator password, and you will be asked to enter to confirm the installation

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

1.1A.2. Install pyenv

Confirm that the latest Homebrew is installed

brew update

Enter the following command to install Pyenv

brew install pyenv

After installation, go directly to Configure environment variables.

1.1B installation using git

Enter the following command in the terminal to install Pyenv

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

If "git: command not found" is prompted, first install git with the following command (you need to enter the administrator password), and then install pyenv with the above command

sudo apt install git

1.2 configuring environment variables

According to the type of shell currently used, you need to enter different command configurations
If you don't know what kind of shell you are currently, you can use the following command to view it

echo $0

bash

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

Ubuntu

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc

Zsh

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc

Fish shell

set -Ux PYENV_ROOT $HOME/.pyenv
set -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths
echo -e '\n\n# pyenv init\nif command -v pyenv 1>/dev/null 2>&1\n  pyenv init - | source\nend' >> ~/.config/fish/config.fish

Make environment variables effective

exec "$SHELL"

Pyenv is now installed

2 install Python

With Python 3 6.8 as an example, if other versions are required, change "3.6.8" to the corresponding version number

pyenv install 3.6.8

Generally, pyenv will automatically download and install some dependent packages first, and then download and install Python, like this

3. Configure Pyenv

Enter the following command in the terminal to view the installed Python version available to Pyenv

pyenv versions

If you follow the above steps, the following should be displayed:

"*" indicates that the current setting is Python provided by the system

Change the current setting to 3.6.8

pyenv global 3.6.8

Confirm the version through "pyenv versions" again as follows:

Note that if you install the python library with pip, it will be installed in the currently set Python version. That is, the libraries installed using pip in this state will be in Python 3.6.8.

4 common Pyenv commands

commandeffect
pyenv versionsList all available versions of Pyenv
pyenv versionDisplays the Python of the current Pyenv setting
pyenv globalSets or displays the current Python version
pyenv installInstalls the specified Python version
pyenv uninstallUninstall the specified Python version

You can use the following commands to view all the supported commands of Pyenv and their function descriptions:

pyenv -h

Keywords: Python

Added by Baseball on Fri, 04 Mar 2022 01:33:10 +0200