Location>code7788 >text

Python Versioning Tool Selection and Pyenv Usage Notes

Popularity:548 ℃/2024-09-05 20:32:28

The main purpose of Python versioning tools is to help developers manage multiple Python versions and environments on the same machine. This is useful for developing and deploying different projects that may rely on different Python versions or different package versions.

Specifically, the Python versioning tool has the following features:

(1) Avoid dependency conflicts. Different projects may depend on different versions of libraries, using version management tools can create independent virtual environments to avoid dependency conflicts.

(2) Streamline the development process by allowing developers to easily switch between versions of Python without having to reinstall or reconfigure Python.

(3) Ease of deployment and reduction of conflicts. Using the same Python version and dependencies in the development environment as in the production environment reduces the number of problems that occur during deployment.

(4) Sharing environment configurations to improve development environment consistency. You can share the environment configuration files (such as maybe) is shared with team members to ensure that everyone is using the same development environment.

Common management tools include Pyenv and Conda. Pyenv is the most popular Python versioning tool, supporting many Python versions, such as CPython, Anaconda, PyPy, etc. It's comprehensive and easy to use. Conda was originally developed by Anaconda, Inc. for package (including Python) and environment management of Python and R programming languages, and is especially suitable for cross-platform and multi-language projects. Python version management is only a small part of its functionality, and if it is only used to manage Python versions, Conda is a bit too small for its purpose, and the system is more complicated and slightly more expensive to learn. In contrast, Pyenv is the best choice for Python versioning in regular projects. Here's how to use Pyenv.

I. Pyenv Installation

Recommendation: Uninstall the system's built-in Python first, otherwise pyenv settings may not take effect.

1. Windows

pyenv itself is designed for Unix systems. You can usepyenv-win This project, which is the Windows version of pyenv.

You need to install pyenv-win by executing the following command in PowerShell:

Invoke-WebRequest -UseBasicParsing -Uri "/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"

Reopen PowerShell and run pyenv --version to check if the installation was successful.

2. Linux

You can use the following command to installpyenv

curl  | bash

After that, configure pyenv to the environment variable and make it effective by executing the following command:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc

The above configuration only enables pyenv in bash environment, for more shell environment configuration, please refer to:Set up your shell environment for PyenvThe essence of the configuration is to configure the shims and bin directories under $PYENV_ROOT into the PATH variable. The essence of the configuration is to configure the shims and bin directories under $PYENV_ROOT into the PATH variable, with shims first. The configured PATH looks like this:

[root@2e7669577b11 /]# echo $PATH
/root/.pyenv/shims:/root/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

II. Pyenv Basic Usage

## Viewing the help file
pyenv

## View the help file for a command
pyenv install --help

## Viewing the version
pyenv version

## Check if Python is running properly
python -c "import sys; print()"

## Check installed Python versions
pyenv versions

## View currently used Python versions
pyenv version

## View all available Python versions
pyenv install --list

## Install the specified version
pyenv install 3.9.1

## Verify
python --version

## Uninstall the specified version
pyenv uninstall 3.9.1

## Specify Python version globally (affects all projects)
pyenv global 3.9.1

## Specify Python version locally (only affects current project directory), create a .python-version file in current project directory to store version information after specification
## Prioritize over global
pyenv local 3.9.1

## Specify Python version at session level (affects all projects)
pyenv shell 3.9.1

## View python installation directory
pyenv which python

## Regenerate the executable in pyenv's shims directory
pyenv rehash

Python Installation Frequently Asked Questions, see:Python common build problems

III. Pyenv Core Principles - Shims

pyenv transparently manages and switches between Python versions through Shims.

1. Principles of operation

In the above environment configuration, insert a shims directory at the top of the PATH environment variable.$(pyenv root)/shims:$(pyenv root)/bin:/usr/local/bin:/usr/bin:/bin. Through a process called rehashing, pyenv maintains shims in that directory to match every Python command in every installed version of Python, e.g. python, pip, etc.

Shims are lightweight executables that simply pass your commands to pyenv. So, with pyenv installed, when you run pip, your operating system will do the following:

(1) Search the PATH environment variable for the pip executable file

(2) Find pip in $(pyenv root)/shims

(3) Run a shim called pip, which passes the command to pyenv

2. Role

(1) By using Shims, pyenv enables flexible management of different Python versions for different projects without the need to manually modify environment variables or paths.

(2) You can easily set or switch Python versions globally, at the directory level, or even at the shell session level, greatly facilitating development and testing.

3. Examples

(1) Suppose you are using Python 3.8 in Project A and Python 3.9 in Project B. With pyenv and Shims, you can set the Python version in the project directory separately:

# in the project A directory
pyenv local 3.8.10

# In project B directory
pyenv local 3.9.5

(2) When you run thepython command, Shims makes sure that Python 3.8.10 is called, while Python 3.9.5 is called in the project B directory.

In this way, Shims enables transparent management and switching between different Python versions.

IV. Pyenv Initialization Operation Source Code Interpretation

1. pyenv init -

Used to initialize pyenv to work in the current shell session. After running it, execute the following command (instructions are included in the comments):

# Variable handling
## This script splits the current PATH variable into an array paths and assigns the
## Checks if each path is '/root/.pyenv/shims' by iterating through the paths array, and removes it if so
PATH="$(bash --norc -ec 'IFS=:; paths=($PATH).
for i in ${!paths[@]}; do
if [[ ${paths[i]} == "''/root/.pyenv/shims''" ]]; then unset '\'\'\'paths[i]'\'';
fi; done.
echo "${paths[*]}"')" #

# 2. Update the PATH variable
## Add '/root/.pyenv/shims' to the top of the PATH variable
export PATH="/root/.pyenv/shims:${PATH}"
## Set the PYENV_SHELL environment variable to bash, and in a sh environment, the output will be a shell.
export PYENV_SHELL=bash
## In sh, there is no such line of code. In bash, the effect of this line is that the source command loads pyenv's autocompletion scripts.
source '/root/.pyenv/libexec/... /completions/'. /completions/'
## Execute pyenv rehash via command (which essentially regenerates the executable in pyenv's shims directory) and redirects error output to /dev/null
command pyenv rehash 2>/dev/null

# 3. Define a pyenv function that performs different actions depending on the subcommand
## If the subcommand is activate, deactivate, rehash, or shell, execute pyenv "sh-$command" via eval.
## For other subcommands, just call command pyenv "$command" "$@"
pyenv() {
  local command
  command="${1:-}"
  if [ "$#" -gt 0 ]; then
    shift
  if [ "$#" -gt 0 ]; then shift

  case "$command" in
  activate|deactivate|rehash|shell)
    eval "$(pyenv "sh-$command" "$@")"
    ;;.
  *)
    command pyenv "$command" "$@"
    ;;;
  esac
}

2. pyenv init --path

Used to set the PYENV_ROOT environment variable so that pyenv can find the installed version of Python.pyenv init - embodypyenv init --path Operation.

After running the sh or bash environment, execute the following command (instructions are included in the comments):

## The script splits the current PATH variable into an array of paths and assigns the
## Checks if each path is '/root/.pyenv/shims' by iterating through the paths array, and removes it if so
PATH="$(bash --norc -ec 'IFS=:; paths=($PATH).
for i in ${!paths[@]}; do
if [[ ${paths[i]} == "''/root/.pyenv/shims''" ]]; then unset '\'\'\'paths[i]'\'';
fi; done.
echo "${paths[*]}"')"
## Add '/root/.pyenv/shims' to the top of the PATH variable
export PATH="/root/.pyenv/shims:${PATH}"
## Execute pyenv rehash via command and redirect error output to /dev/null
command pyenv rehash 2>/dev/null

V. Reference articles

pyenv

Python common build problems