Use pyenv to manage Python versions

pyenv switches between different versions of Python by modifying environment variables in the system.

The beauty of pyenv is that instead of using the highly coupled way of putting different PATHs into different shells, it simply inserts a shim at the top of the PATH: ~/. pyenv/shims:/usr/local/bin:/usr/bin:/bin. All searches for Python executables are first intercepted by this shims path, invalidating the system paths behind.

1 Install the latest version of pyenv to the ~/.pyenv directory (other directories are fine)

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

2 Add three lines of instructions to ~/.bash_profile so that the system can find the Python installed by pyenv

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile

echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

3 Restart your shell for the 3 instructions added above to take effect

exec $SHELL

4 Install any Python version you need with pyenv

pyenv install 3.5.5

5 View all Python versions currently in the system

pyenv versions # pyenv version View current version

6 Set a version to be used globally

pyenv global system

7 Set a version to the current directory to take effect

pyenv local 3.5.5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325171529&siteId=291194637
Recommended