Pyenv introduction

Pyenv is a Python version management tool. Pyenv can change global Python versions, install multiple versions of Python, set directory-level Versions of Python, and create and manage virtual Python Environments. All Settings are user-level and do not require the sudo command.

Pyenv is mainly used to manage versions of Python, such as a project requiring Python 2.x and a project requiring Python 3.x. Virtualenv is mainly used to manage Python package dependencies. Different projects need to rely on different package versions, so you need to use virtual environments.

Introduction to pyEnv

Pyenv lets you switch between Python versions by systematically modifying environment variables. Virtualenv acts as the Python package virtual environment by installing Python packages into a directory, and switching between different package environments by switching directories.

The beauty of PyEnv is that, rather than working in a highly coupled way of planting different paths into different shells, it simply inserts a shim PATH (shims) at the top of the PATH: ~ /. Pyenv/shims: / usr/local/bin: / usr/bin: / bin. All look-ups of Python executables are first intercepted by this shims path, invalidating the system path behind them.

Prior to installation

For different operating systems, see Common Build Problems to install required tools.

Pyenv installation

Follow the installation instructions on the official website or install it automatically. If using a Mac, use Homebrew directly. After the installation is successful, add three lines to.bashrc or.bash_profile to enable auto-completion.

Configure according to your environment
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Copy the code

Automatic installation

Pyenv provides an automatic installation tool, which can be installed by executing the following command:

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
Copy the code

Ensure that the system has Git; otherwise, a new Git installation is required.

Manual installation

If you want to learn more about the installation process, you can use manual installation. Check out PyEnv to the directory you want to install. The recommended path is $HOME/.pyenv

cd ~
git clone git://github.com/yyuu/pyenv.git .pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc
Copy the code

Add the environment variable PYENV_ROOT to the root directory checked out by Pyenv, and add $PYENV_ROOT/bin to $PATH to provide the PATH to the Pyenv command.

The shell configuration file (~/.bash_profile) here needs to be modified depending on Linux, and the corresponding ~/.zshrc configuration is required if ZSH is used

Third party modules installed using PIP after using PyEnv are automatically installed in the current version of Python and do not conflict with system modules. After installing the module with PIP, remember to use PyEnv ReHash to update the shim path if it doesn’t work.

Pyenv common commands

Use Pyenv Commands to display all available commands

Python configuration

View the list of python currently installed on the system
pyenv versions

# list installable versions
pyenv install --list 

Install the specified version of Python
pyenv install -v 3.5.1

Uninstall the specified version of Python
pyenv uninstall 2.7.3

Display the current Python installation path
pyenv which python 

Create spacer path
Create shims for all installed executables
#, such as: ~ /. Pyenv/versions / * / bin / *
This command should be executed every time you add or remove a Python version or a package with an executable (such as PIP)
pyenv rehash
Copy the code

Python switch

Set the global Python version by writing the version number to the ~/. Pyenv /version filePyenv Global < version number >Set the application-oriented local version by writing the version number to the.python-version file in the current directory. The Python version set in this way has a higher priority than global.
# pyenv looks for.python-version files from the current directory up to the root directory. If you can't find it, use the global version.Pyenv local < version >Set the shell-oriented Version of Python by setting the PYENV_VERSION environment variable for the current shell. This version has a higher priority than local and Global.Pyenv shell < version number >The unset argument can be used to cancel the current version of the shell.
pyenv shell --unset
Copy the code

Python priority

shell > local > global

pyenv-virtualenv

Pyenv-virtualenv pyenv-virtualEnv pyenv-virtualEnv pyenv-virtualEnv pyenv-VirtualEnv pyenv-VirtualEnv

The installation

With automatic Installation of PyEnv, it automatically installs some plug-ins, including Pyenv-VirtualEnv.

To install pyenv-virtualenv using Homebrew, run brew install pyenv-virtualenv to install pyenv-virtualenv.

After this installation, you need to perform the following configuration:

#With ZSH shell
vim ~/.zshrc

#Use system Defaults
vim ~/.bash_profile

#At the end of the '.zshrc 'or'.bash_profile 'file write:
# pyenv-virtualenv
if which pyenv-virtualenv-init > /dev/null;
  then eval "$(pyenv virtualenv-init -)";
fi

#Make the configuration take effect
source ~/.zshrc
# or
source ~/.bash_profile
Copy the code

use

Create a virtual environment
# If the Python version is not specified, the current Python version is used by defaultPyenv virtualenv <Python version number ># list the current virtual environment
pyenv virtualenvs

Activate the virtual environmentPyenv Activate < virtual environment name >Exit the virtual environment
pyenv deactivate

Delete the virtual environmentPyenv Uninstall < Virtual environment name >Copy the code

Tips: Replace the PIP source

Vim ~/. PIP/PIP. Conf = vim ~/. PIP/PIP.

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
Copy the code

PIP

Douban: http://pypi.douban.com ali cloud: http://mirrors.aliyun.com/pypi/simple tsinghua university: https://pypi.tuna.tsinghua.edu.cn/simple

reference

  • pyenv GitHub
  • Manage Python versions using PyEnv
  • Create multiple versions of Python using PyEnv + VirtualEnv
  • Install and use PyEnv and Pyenv-VirtualEnv on Mac