Freely switch between multiple versions of Python

Posted Jun 26, 20205 min read

1 Overview

There is a thunderstorm in the Dragon Boat Festival, it is not easy to go out, there is a python program to debug, the build environment is python 2.7.5, an older environment, and the new program is written based on python3, it seems to switch to the old version of python Debugging, if you want to easily switch the python version, think of the python version switching tool. After searching online, pyenv can do this job. pyenv supports linux and windows, and the support under linux is more perfect. I am win10. The following practical operation will take win10 as an example.

2. Practice record

  • Install pyenv

The machine originally installed python3.8.x, using pip to install pyenv, pip install pyenv.

  • Configure environment variables

In order to access pyenv at any time under windows, you need to add the installed pyenv execution file to the path, and you also need to add the pyenv shims gasket directory(this term is quite magical, in fact, as I will talk about later, this operation is really amazing And listen to the decomposition later).
Add PYENV environment changes to system variables, my values are as follows:
PYENV = C:\Users\Admin\AppData\Local\Programs\Python\pyenv-win

Add the path of pyenvr.bat to the system path variable, and also add the shims shim directory to the path variable. After completion, as shown below:
PATH =%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\ ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;C:\Program Files\nodejs\;%PYENV%\bin;%PYENV%\shims;

The text is not expressive, the above picture:

NreQRf.png

At this point, pyenv has been successfully installed and is available. Open a command window(win+R, enter cmd and press Enter), enter pyenv, and the version number and common commands of pyenv will be displayed. The installation is successful.

>pyenv
pyenv 1.2.4

Usage:pyenv <command> [<args>]

Some useful pyenv commands are:
   commands List all available pyenv commands
   duplicate Creates a duplicate python environment
   local Set or show the local application-specific Python version
   global Set or show the global Python version
   shell Set or show the shell-specific Python version
   install Install a Python version using python-build
   uninstall Uninstall a specific Python version
   rehash Rehash pyenv shims(run this after installing executables)
   version Show the current Python version and its origin
   versions List all Python versions available to pyenv
   exec Runs an executable by first preparing PATH so that the selected Python
   which Display the full path to an executable
   whence List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see:https://github.com/pyenv-win/pyenv-win#readme
  • Install different python versions

pyenv install --list
View installable python version

pyenv install 3.8.0
Install python 3.8.0

pyenv install 2.7.5
Install python 2.7.5

Update pyenv command:
pyenv rehash

  • Switch python version

I hope to use python 3.8.0 version at ordinary times, set the commonly used version to this version, the command is as follows:pyenv global 3.8.0, after running, when using python in the future, it will use this version by default.

I want to debug the old version of the python program. In this directory, a specific python version is required. Therefore, a specific version is set in this directory. The command is as follows:pyenv local 2.7.5, so that as long as you enter this directory, run Python is python 2.7.5.

>python
Python 2.7.5(default, May 15 2013, 22:43:36) [MSC v.1500 32 bit(Intel)]on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
  • Install pip

Install setuptools first, download: https://pypi.org/project/setu... , unzip, and run python setup.py install.
Then install pip, download: https://pypi.org/project/pip/... , unzip it, and run python setup.py install.
Update pyenv command:pyenv rehash

3. Knowledge of other parameters

  • How pyenv works

The working principle of pyenv is actually very simple. Insert the Python directory managed by pyenv into environment variables, so that the first thing the system finds when searching for Python is the Python environment managed by pyenv. The path inserted to the front is called the shims, which is the%PYENV%shims directory.

When the pyenv command is executed, it will have three levels of global(current system), local(current directory), and shell to manage the Python environment. The Python version to be used is determined in the following order:
Priority shell> local> global

If the PYENV_VERSION environment variable exists, the python version specified by the variable is used. You can use the pyenv shell command to set this environment variable in the current shell environment.

If there is a file in the current directory:.python-version, the python version specified by this variable is used. You can use the pyenv local command to modify the .python-version file in the current directory.

If the above 2 variables do not exist, then search the upper-level directories of each layer from bottom to top. The first one found. python-version is the python version to be used, until it reaches the file system root directory, which is the global $(pyenv root)/version file. It can be modified using the pyenv global command. If the global version file does not exist, pyenv assumes that you are using Python installed on the system.(In other words, Python found in environment variables when pyenv is not installed).

Then use pip to install the software, fast and easy.

  • pyenv other commonly used commands

pyenv shims displays files under shims
pyenv rehash generates shims directory and related files, such as python, pip, etc.
pyenv install --list View installable python versions
pyenv install 3.8.0 install python 3.8.0 version
pyenv global 3.8.0 specifies the version number of python running globally
pyenv local 2.7.5 Generate a python version file in the current directory. When running python in this directory, run the version specified in the version file
pyenv shell 2.7.5 specifies the version of python to run in the current command window, the specified version will be used when running python in this window
pyenv shell --unset remove the specified python version in the current command window, the local or global set python version will be used
pyenv version View the currently activated version. The content in parentheses indicates which way this version is activated(global, local, shell)
pyenv versions View all the currently installed versions of pyenv. The active version is marked with *.

>pyenv versions
* 2.7.5(set by D:\01xxx\source\python\.python-version)
  3.8.0

pyenv uninstall 2.7.5 uninstall a version

pyenv rehash creates shims for all installed executable files(eg:~/pyenv/versions/*/bin/*), so whenever you add or delete Python versions or packages with executable files(eg pip) In the future, this command should be executed once
Main execution code:pyenv-winlibexecpyenv.vbs
The installation path of each python version of pyenv:pyenv-winversions