Python switch version artifact pyenv

Keywords: Python shell brew Session

Because some syntax uses of Python 2.x and 3.x are different, you will encounter the need to switch versions when writing and using Python programs. The following describes pyenv, a tool for switching Python versions.

Install pyenv

Take mac as an example. You need to install brew first. If you don't, you need to install brew and pyenv first. Now you need to set the agent on the terminal to install brew and pyenv.

brew update
brew install pyenv

Configure zsh

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
zsh

Install and view py version

# View installable versions
pyenv install -l

# Install and uninstall python 3.6.6. Note that you need to set up the terminal agent to install the py version, otherwise it may time out and cause failure
pyenv install 3.6.6
pyenv uninstall 3.6.6

# View the current Python version
pyenv version

# View the installed Python version
pyenv versions

The installed version of py is in the / Users / your username /. pyenv/versions directory.

➜  versions pwd
/Users/thoth/.pyenv/versions
➜  versions ls
3.6.6
➜  versions

Switch py version

# Global global settings generally do not recommend changing global settings
pyenv global <python Edition>

# Shell session settings only affect the current shell session
pyenv shell <python Edition>
# Unset shell session
pyenv shell --unset

# Local local settings only affect the folder
pyenv local <python Edition>

The priority relationship is: shell > local > Global

Here is an experiment. Write a small script and run it:
seeversion.py

import sys
print(sys.version)
print(sys.version_info)

Verify switch to 3.3.6

# Set version
➜  program pyenv shell 3.6.6

# View version
➜  program pyenv versions
  system
* 3.6.6 (set by /Users/thoth/program/.python-version)

# Run script as expected
➜  program python seeversion.py
2.7.16 (default, Dec 13 2019, 18:00:32)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s
sys.version_info(major=2, minor=7, micro=16, releaselevel='final', serial=0)

Verify switch back to system default version

# Set version
➜  pyenv local system

# View version
➜  ~ pyenv versions
* system (set by /Users/thoth/.python-version)
  3.6.6

# Run script as expected
➜  ~ python seeversion.py
2.7.16 (default, Dec 13 2019, 18:00:32)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s
sys.version_info(major=2, minor=7, micro=16, releaselevel='final', serial=0)

Conclusion:
I feel that global is the directory you run in the terminal, which is effective, but this setting is too wasteful. After all, we usually only run py scripts in a few fixed directories.

shell means that the terminal you are currently opening is valid, and it is invalid after closing.

local is, for example, if you cd into the program directory, it is valid in the current. Program directory. Other directories are invalid.


Use example with ide

To let the ide run the version you installed, just set the running version path to / Users/thoth/.pyenv/versions/3.6.6/bin/python.


Some differences with virtualenv

pyenv can easily switch between different versions of terminal and ide. But for developers who need to develop different py programs, different projects mean that each project has different extended class libraries, which are all installed in the corresponding version of python environment, making them feel uncomfortable, inconvenient to manage, and bloated. It is hoped that the environment of each project is independent, pure and clean. With this critical need, virtualenv was born for it.


Reference

https://www.sqlsec.com/2019/12/pyenv.html#toc-heading-5
https://www.jianshu.com/p/8aaf2525fa80

Posted by DarkJamie on Tue, 24 Mar 2020 00:08:07 -0700