Pyenv virtual environment tools to install, configure and use, perfect control of python version


Foreword

Use python development, Unix / Linux environment will be more friendly and inexplicable bug does not appear, although the windows store also released Ubuntu subsystem, but the function is not complete, the system is not perfect. This article describes a comprehensive Ubuntu, Archlinux, Centos, Mac under the installation and the methods used pyenv. Under Windows method pyenv of the venue pyenv-win Source Address View: https://github.com/pyenv-win/pyenv-win , of course pyenv also be used in Windows store of Linux subsystem.


background

python version more, 2 and 3 are relatively large difference, when you need different environments, choose to use docker, docker use of tutorials can be found in the rookie tutorial

https://www.runoob.com/docker/docker-tutorial.html

You can also find the relevant documents in the official website

https://www.docker.com/

If docker it does not need an isolated environment, and if you want to run directly on the server, it must be isolated from the environment. This article will introduce the use pyenv to create different versions of python virtual environment.


use

Use pyenv and pyenv-virtualenv, in perfect isolation under Linux versions python

Chapter 1: Using the environment

Note: conducted under this article Linux and Mac OS X, it does not involve the installation under windows

Inventory

  • git
  • bash, zsh or brew
  • pyenv
  • pyenv-virtualenv

1, install git

Git installed at all major Linux distributions are very simple, here only show some examples of

  • archlinux
    sudo pacman -S git
  • debian/ubuntu
    sudo apt install git
  • CentOS
    sudo yum install git
  • Mac
    brew install git

2, open terminal

Under Ubuntu using shell

Ctrl+ Alt+ tShortcut to open the terminal

3, installation pyenv


Note: All installation of strict compliance with this article are official documents, and official documents entirely consistent.

Source git Address: https://github.com/pyenv/pyenv

Run the following commands to complete the installation in the terminal:

First, the project clone it, and put the hidden files in the home directory folder: .pyenv

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

If you are a Mac user, you can use the command to install brew

brew update
brew install pyenv

Then configure the environment variables:

  • If you use bash, in turn execute the following command:
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
  • If you use zsh, in turn execute the following command:
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
  • If you use a Mac, you execute the following command:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init-)"\nfi' >> ~/.bash_profile

Meaning echo command is: will write the contents of a file in quotation marks in

Please note that the last command more than three echo command, ensure that the content it quotes in ~ / .bashrc or ~ / .zshrc the bottom.

Because during pyenv initialization operation path environment variable, leading to unpredictable behavior.

Bottom view the contents of a file, you can use the tail command usage: tail ~ / .bashrc or tail ~ / .zshrc, you can edit the file using vim or vscode

Then, before use pyenv, you need to initialize the shell environment, execute the following command

exec $SHELLorsource ~/.bashrc

The same can also close the current terminal window, restart one.

4, the installation pyenv-virtualenv

Source git Address: https://github.com/pyenv/pyenv-virtualenv

The plug-in pyenv cloning have already installed the plugins folder

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

If you are a Mac user can use the command to install brew

brew install pyenv-virtualenv

Then configure the environment variables

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  • If you use bash, you execute the following command:

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

  • If you use zsh, then execute the following command:

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc

Finally, before use pyenv, re-initialize the shell environment, execute the following command

exec $SHELLorsource ~/.bashrc

Or restart a terminal window.

No wrong, now you have completed all installation operations, if an error, please check earlier in thepathwithEnvironment Variables( Check to see if the system environment using the corresponding commands ) is configured correctly, or the operation to roll back and re-run the install command


Chapter 2: Using pyenv

Shown here only daily usage of pyenv and pyenv-virtualenv

Check for proper installation

  • Check the version pyenv

pyenv version

  • View pyenv which has hosted the python version

pyenv versions

If the normal display version information, indicating a successful installation, if similar to the display command not found tips and the like, the installation fails, review the first chapter of the end of the prompt.

Install version 3.6.6 of python

pyenv install 3.6.6

It should be noted that the installation will fail, Build failed in some cases.

Response options Do not panic, we can look at github wiki pyenv which prepared the original address https://github.com/pyenv/pyenv/wiki

Excerpted below, just execute the corresponding command:

  • archlinux users

sudo pacman -S base-devel openssl zlib

  • Mac users

brew install openssl readline sqlite3 xz zlib

If you are using OS X Mojave or higher version (10.14+), also you need to execute the following command
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

  • Ubuntu / Debian / Mint users

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev

  • CentOS / Fedora <= 21 users, to ensure that the tools are installed xz

sudo yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel

  • Fedora> = 22 users, to ensure that the tool is already installed xz

sudo dnf install -y gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel

  • openSUSE users

zypper install gcc automake openssl-devel ncurses-devel readline-devel zlib-devel tk-devel

  • Solus user

sudo eopkg it -c system.devel

sudo eopkg install git gcc make zlib-devel bzip2-devel readline-devel sqlite3-devel openssl-devel tk-devel

  • Linuxbrew user

brew install bzip2 openssl readline sqlite xz

These additional tools after you have installed, execute again:

pyenv install 3.6.6

To be successful, you can continue to use pyenv versionsto view the pyenv hosted python version, not the conflict between multiple versions, perfect coexistence

Execution pyenv install, and then press the tabkey, you can see all the optional mounting version

How to create a virtual environment using already installed version

Collaboration pyenv and pyenv-virtualenv can be so understanding:

pyenv managed installation of python version, python-virtualenv the corresponding call python version

Here's how to create and use virtual environments

1. Create a virtual environment

First you need to create a virtual environment, execute the command:

pyenv virtualenv 3.6.6 my-env

The last one is the name of the environment, you can easily take. Successful installation will appear:

Looking in links: /tmp/tmp0eywgc7v
Requirement already satisfied: setuptools in /home/vic/.pyenv/versions/3.6.6/envs/my-env/lib/python3.6/site-packages (39.0.1)
Requirement already satisfied: pip in /home/vic/.pyenv/versions/3.6.6/envs/my-env/lib/python3.6/site-packages (10.0.1)

It stated the absolute path of the virtual environment.

2. Activate the virtual environment

In any directory, execute the command:

pyenv activate my-env

In the terminal command line front, there will be (my-env)prompt, enter python environment can see the version 3.6.6 of the virtual environment settings, use pip --version, you can see the absolute path pip packages installed

Into the terminal using any directory, such as ~ / envtest

cd ~/envtest

Then execute in that directory:

pyenv local my-env

You can also activate the created virtual environment, execute:

ls -al

Command difference between local and before that, in ~ / under envtest directory, creates a hidden file .python-version, the contents of the file contains only one sentence my-env, just use the terminal to enter ~ / test directory, it will automatically activate the virtual surroundings

Exit virtual environment using the following two commands in any one

pyenv deactivate  # 或者
source deactivate

3. Delete the virtual environment

Over time, you may accumulate a lot of virtual environments, delete the virtual environment can use the command

pyenv uninstall my-envOr rm -rf my-envremove the directory where the virtual environment


Chapter 3: Update pyenv

If you use git clone update command using git pull

cd ~/.pyenv or cd $(pyenv root)

git pull origin master

Mac users will use the brew command

brew upgrade pyenv


Chapter 4: Uninstall pyenv

Since pyenv put everything in ~ / down .pyenv, so the next two steps to uninstall Linux

  1. To delete an environment variable

  2. Execution: rm -rf ~/.pyenvorrm -rf $(pyenv root)

Mac users need to use the brew to uninstall command:

brew uninstall pyenv






PS

This reference to the official documentation and been modified document can be found at github source address

pyenv: https://github.com/pyenv/pyenv
pyenv-virtualenv: https://github.com/pyenv/pyenv-virtualenv

Guess you like

Origin blog.csdn.net/qq_27114273/article/details/90340754