Pyenv make the python version switch perfectly

Source: Internet
Author: User
Tags documentation install openssl openssl readline sqlite centos virtual environment virtualenv
Objective

I think if you use Python to develop, or in the unix/linux environment, the shell tool is much more efficient than windows, even though there are cmder artifacts under Windows, and now the Windows Store is well developed Li Nux subsystem, but the flaw is very many, Unix/linux is the perfect environment. This article uses the ArchLinux, in addition Debian/ubuntu, CentOS also completely applies. Mac users can also refer to it, but homebrew also provides a very convenient way to install it, but it is recommended to use the text approach.

Background

Python version is more, 2 and 3 is very big difference, many projects need to run on the same server, we can choose to run directly, you can choose to use Docker. If you use Docker, you do not need to isolate the environment, if you want to run directly on the server, it must have an isolated environment. For example, some projects use Python 3.5, some of which use Python 3.7, and we can help us to isolate the environment perfectly by using pyenv, so that multiple versions of Python have no conflict and perfectly coexist.

Task

Use Pyenv and pyenv-virtualenv to perfectly isolate Python versions under Linux

Chapter 1th: Use of the environment
    • Operating system: ArchLinux
    • Shell:zsh

Note that all of the next operations are under ArchLinux, and this article does not cover Windows

Listing
    1. Git
    2. zsh or Bash
    3. Pyenv
    4. Pyenv-virtualenv
1. Install git

It's easy to install git on major Linux distributions, only a few examples are shown here

ArchLinux

sudo pacman -S git

Debian/ubuntu

sudo apt-get install git

Centos

sudo yum install git

2. Open Terminal

This article uses zsh

3, Installation Pyenv

Note: All installations in this article are strictly in accordance with official documentation and are fully consistent with official documentation.

Git address: github.com/pyenv/pyenv

In your terminal to execute the following command, safe and non-toxic, please feel free to eat:

First clone the item and put it in the hidden folder in the home directory:. pyenv

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

Then configure the environment variables

If you use Bash, execute the following command in turn:

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 are using ZSH, execute the following command in turn:

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' >> ~/.bashrc

The echo command means that the contents of the quotation mark are written to a file
Please note that the last long command of the three echo commands above, please ensure that the contents of the quotation marks are at the bottom of ~/.BASHRC or ~/.ZSHRC.
Because the PATH environment variable is manipulated during pyenv initialization, it causes unpredictable behavior.
To view the contents of the file at the bottom, you can use the tail command, using: Tail ~/.BASHRC or tail ~/.zshrc, editing the file can use Vim or Vscode

Finally, before using pyenv, reinitialize the shell environment and execute the following command

exec $SHELL

It is perfectly possible not to execute the command, you can close the current terminal window and reboot one.

At this point, you have completed the installation of pyenv, you can use it all the commands, but I suggest you do not rush to use, a breath of pyenv a plug-in, that is pyenv-virtualenv

4, Installation Pyenv-virtualenv

Git address: github.com/pyenv/pyenv-virtualenv

Clone the plugin in the Plugins folder of the pyenv that you have just installed

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

Then configure the environment variables

If you use Bash, execute the following command:

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

If you use Zsh, execute the following command:

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

Finally, before using pyenv, reinitialize the shell environment and execute the following command

exec $SHELL

It is perfectly possible not to execute the command, you can close the current terminal window and reboot one.

Here, all of our important installations have been completed and we can begin to experience them.

2nd Chapter: Using PYENV

This shows only the daily usage of pyenv and virtualenv.

Check that the installation is correct

Check the version of Pyenv

pyenv version

See which Python versions the pyenv has hosted

pyenv versions

If you see the normal version information, it is OK, if you see something like command not found, it means that the installation failed.

Install 3.6.6 version of Python

pyenv install 3.6.6

Here's the problem, in some cases the installation fails, and an error tells you Build failed

At this point, PYENV has prepared an error response for us in its GitHub wiki, with the original address Github.com/pyenv/pyenv/wiki

To do this, just execute the corresponding command:

ArchLinux Users

sudo pacman -S base-devel openssl zlib

Mac users

brew install openssl readline sqlite3 xz zlib

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, please ensure that the XZ tool is installed

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

Fedora >= 22 Users, make sure you have the XZ tool installed

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

OpenSUSE User

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

Solus Users

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 Users

brew install bzip2 openssl readline sqlite xz

After installing these supplemental tools, execute again:

pyenv install 3.6.6

Can be successful, you can continue to use

pyenv versions

To view the Python version hosted by pyenv

And you want to install what version of what version, want to install a few, are perfect coexistence, perfect isolation, you can enter in the terminal

pyenv install

Then press the TAB key to see all the optional installation versions.

Using the Python 3.6.6 you just installed

First we need to clarify a concept, pyenv and pyenv-virtualenv how they work together, as you can think:

Pyenv hosted Python version, virtualenv using Python version

Well, I've already installed the version, so let's use it now.

1th Step: Create a virtual environment

You first need to create a virtual environment to execute the command:

pyenv virtualenv 3.6.6 my-env

It's formatted like this, and the last one is the name of the environment you want, and you can take it anywhere. Wait a moment, and you'll see:

Looking in Links:/tmp/tmp0eywgc7v
Requirement already satisfied:setuptools in/home/joit/.pyenv/versions/3.6.6/envs/my-env/lib/python3.6/ Site-packages (39.0.1)
Requirement already SATISFIED:PIP in/home/joit/.pyenv/versions/3.6.6/envs/my-env/lib/python3.6/site-packages ( 10.0.1)

Similar to this echo information, the environment has been created successfully, it also tells you the absolute path of the virtual environment, if you look in, you will find that the so-called virtual environment, is to put Python in a folder in the Pyenv installation directory for its own invocation.

2nd step: Activating the virtual environment

Under any directory, execute the command:

pyenv activate my-env

You will find that there is one more thing in your terminal that resembles (my-env) this, when you execute:

python --version

That's Python 3.6.6.

If you do:

pip --version

It will tell you the absolute path of the PIP package installation and is also a folder in the Pyenv installation directory

If you turn off the terminal, you have to reactivate it again the next time you start, you can use the following command:

First CD to a directory, such as ~/test

cd ~/test

Then execute it in this directory:

pyenv local my-env

You will find that it has been activated, so what is the difference between the local command and just now? If you do:

ls -al

You will find that there is a hidden file in the ~/test directory. Python-version, you can see the file inside, only write a word my-env

This will automatically activate the virtual environment as soon as you enter the ~/test directory.

In a virtual environment, if you execute directly

python

It goes into Python's interactive environment.

If you write a file with the name app.py, there is only one code in the content: print (1)

Then execute:

python app.py

At this point, the system invokes the Python interpreter in the virtual environment to execute the code.

The 3rd chapter: Update pyenv

Since we are a git clone, the update is very simple

cd ~/.pyenvOrcd $(pyenv root)
git pull

4th Chapter: Unloading pyenv

Because Pyenv put everything under the ~/.pyenv, so the unloading is very convenient, two steps on the line

First you need to delete the environment variable

Then you need to perform:

rm -rf ~/.pyenvOrrm -rf $(pyenv root)

My public number.

Name: Mani Small Code
Number: Mannycoder
Two-dimensional code:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.