This is the 27th day of my participation in the August More Text Challenge

We may use different Versions of Python in our daily project development or study. As we all know, there are many versions of Python. If we download all the different versions of Python we need to the server, it will be very difficult to manage. It is possible that the entire Python environment on the server will be cluttered and not work properly, so how can we solve the problem of multiple versions of Python being managed? Pyenv, a Python environment management tool, allows you to easily switch between multiple versions of Python without worrying about version clutter.

Installation and Configuration

Pyenv is now open source on Github. We clone the pyEnv project directly from Github:

git clone https://github.com/pyenv/pyenv.git  ~/.pyenv
Copy the code

Then, execute the following commands respectively to install: then append environment variables (path to PyEnv, etc.) to the configuration file

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init --path)"' >> ~/.profile
Copy the code

After environment variables are installed and configured, you need to reload the configuration file to make the newly added configuration take effect. The source command is usually used to re-execute the newly modified initialization file to make it take effect immediately without logging out and re-logging in. As follows:

source ~/.bash_profile
Copy the code

Pyenv is installed and can be verified by using the following command: pyEnv

pyenv --help
Copy the code

Using pyenv

The install command for Pyenv can be used to check which Python versions pyEnv currently supports, as shown below:

pyenv install --list
Copy the code

Note: the screenshot here is not complete, there are many versions.

You can use the pyenv versions command to view the Python versions contained in the current system:

pyenv versions
Copy the code

We haven’t installed any other Python versions using PyEnv here, so we’ll just use one of the native Python versions of the system.

Pyenv Install can be used to install different versions of Python:

Pyenv Install 3.7.4 Pyenv Install 3.8.0Copy the code

When installing Python using the Pyenv install command, the default version of python.org will be downloaded very slowly and will be stuck here:



You can start by downloading the specified Python version from a domestic mirror site~/.pyenv/cachePyenv install directory, and then use the pyenv install command to install. Taobao source is used here.

Wget https://npm.taobao.org/mirrors/python/3.7.4/Python-3.7.4.tar.xz - P ~ /. Pyenv/cacheCopy the code

If you run the Pyenv install command again after downloading from a domestic source, you will skip the download and install directly.

After the installation is complete, run PyEnv again to display the Python version you just installed.

Two Python versions, 3.7.4 and 3.8.0, have been installed, and the Python version of the system has been installed. The “*” in front of the output indicates the version that is being used.

We can change the Python version of the global using PyEnv Global, as follows:

Pyenv global 3.7.4Copy the code

If you don’t want the specified Python version to work globally, but in the specified directory, you can use the pyenv local command to change it:

Pyenv local 3.7.4Copy the code

This command only changes the Python environment in the directory where the name is executed.

If you want to remove the Python version, use the uninstall command. As follows:

Pyenv uninstall 3.7.4Copy the code

conclusion

With PyEnv, you can quickly switch versions of Python, and version-dependent dependencies can be switched as well, so there is no problem of multiple versions co-existing and interfering with each other. Use PyEnv install, local, Global, and Uninstall to install and remove Python versions. If you are interested, you can learn more about other ways to use PyEnv.

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!