preface

When dealing with multiple Python development projects, you need to create a development environment for each project. Typically, virtualEnv is used to create a virtual standalone Python environment, but the environments virtualEnv creates are relatively scattered and difficult to manage. Virtualenvwrapper is recommended to create a centralized, manageable Python environment, and pyEnv can be used to select different Versions of Python for different projects.

virtualenvwrapper

The installation

Install VirtualenvWrapper via PIP (Ps: remember to add sudo).

$ sudo pip install virtualenvwrapperCopy the code

configuration

Add the following to.bashrc(or.zshrc) :

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.shCopy the code

Then execute ‘source ~/.bashrc(or./ ZSHRC). Note: Change.bashrc or.zshrc depending on which shell you are using.

use

  • workon: Prints all virtual environments.
  • mkvirtualenv xxx: Create XXX virtual environment;
  • workon xxx: use XXX virtual environment;
  • deactivate: Exits the XXX virtual environment.
  • rmvirtualenv xxx: Deletes the XXX virtual environment.

pyenv

The installation

Linux

Install Pyenv automatically using the following command:

$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bashCopy the code

Mac OS X

Install directly with Homebrew:

$ brew update
$ brew install pyenvCopy the code

configuration

Add the following to.bashrc(or.zshrc) :

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"Copy the code

Then restart the shell for the configuration to take effect.

$ exec "$SHELL"Copy the code

Install Python depending on the environment

When installing Python, you need to have the required dependency environment installed in advance.

  • Ubuntu/Debian:
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \ libreadline-dev libsqlite3-dev wget curl  llvm libncurses5-dev libncursesw5-dev \ xz-utils tk-dev libffi-dev liblzma-devCopy the code

  • Fedora/CentOS/RHEL:
sudo yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel \
openssl-devel xz xz-devel libffi-develCopy the code

Alternative of openssl-devel:

sudo yum install compat-openssl10-devel --allowerasingCopy the code

  • openSUSE
zypper in zlib-devel bzip2 libbz2-devel readline-devel sqlite3 sqlite3-devel libopenssl-devel xz xz-develCopy the code

  • macOS:
brew install readline xzCopy the code

use

1. View all Python versions

$ pyenv versionsCopy the code

Executing the command will print out all installed Python versions, with * indicating the current Python version in use.

2. Query all available Python versions

$ pyenv install -lCopy the code

3. Installation and uninstallation

$pyenv install 2.7.14Copy the code

Install Python version 2.7.14.

$pyenv uninstall 2.7.14Copy the code

Uninstall Python version 2.7.14

$ pyenv rehashCopy the code

Create shims for all installed executables, such as ~/.pyenv/versions//bin/, so you should run this command whenever you add or delete Python versions or packages with executables (such as PIP).

4. Set the Python version

$pyenv shell 2.7.14 # Set the shell-oriented Version of Python by setting the current shell's PYENV_VERSION environment variable. $pyenv local 2.7.14 # Set the local version of Python 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 global 2.7.14 # Set the global Python version by writing the version number to the ~/. Pyenv /version file.Copy the code

Note: shell > local > 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.

Ps: Try not to modify Global because systems often rely on native Python versions.

5. Cancel the shell or Local Python version

$ pyenv shell --unset
$ pyenv local --unsetCopy the code

Use images to speed up downloads

Use the following script to quickly install each version of Python using a domestic sohu image:

V = 3.6.3 wget | http://mirrors.sohu.com/python/$v/Python-$v.tar.xz - P $(pyenv root)/cache /. pyenv install $vCopy the code

Note: v is the Python version number. Change it as required.

$(pyenv root)/cache/ pyenv install XXX (pyenv root)

pyenv-virtualenvwrapper

The installation

Linux

Install Pyenv-Virtualenvwrapper with the following command:

$ git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git $(pyenv root)/plugins/pyenv-virtualenvwrapperCopy the code

Mac OS X

For Mac OS X, you can optionally use Homebrew to install:

$ brew install pyenv-virtualenvwrapperCopy the code

The activation

After installation, activate Pyenv-Virtualenvwrapper using the following command:

$ pyenv virtualenvwrapperCopy the code

use

$PIP install virtualenvwrapper $pyenv shell 3.6.3 # install virtualenvwrapper $mkvirtualenv python3 $mkvirtualenv python3 $mkvirtualenv python3Copy the code

reference

  1. Pyenv document
  2. Virtualenvwrapper document
  3. Pyenv – virtualenvwrapper document
  4. Manage Python versions using PyEnv
  5. Pyenv setup and configuration is accelerated with domestic mirroring