Solve the problem of Python switching version

  • 2021-11-14 06:25:18
  • OfStack

Catalog Overview
Other Platform 1, Install Homebrew
2. Install pyenv with the brew command
3. Install Python 3.7. 5 with pyenv
4. Solve the problem of slow installation speed of Python with pip command, and use domestic mirror image instead
5. The switching version is invalid, try bash
Reference

Overview

Because mac will install python2.x by default, which causes inconvenience to our python development. We often use python3.x version for testing and development. For example, if there are 2. x and 3. x open source projects, we must switch back and forth to use different versions of python for testing and development, which is very inconvenient. Therefore, we have to install pyenv on the computer and switch python version freely, similar to nvm and python version managers in nodejs. Next, Grandma began to introduce how to install and how to solve some problems.

Other platforms

windows: Reference 1 linux: For Ubuntu 16.04 LTS, Python2 (2.7. 12) and Python3 (3.5. 2) are installed simultaneously by default, and the default python version is 2.7. 12.

1. Install Homebrew

Terminal execution (official website)


/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

2. Install pyenv with brew command

1) Command-line input:


#  It's best to update it before installing it. It hasn't been updated for too long homebrew May not be able to install 
brew update
#  Installation pyenv At present, the latest 2.x
brew install pyenv

2) Modify the ~/. zshrc file in the home directory


vi ~/.zshrc Add at the end 
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/shims:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
 eval "$(pyenv init -)"
fi

3) Command line input, update environment variable under 1


source ~/.zshrc , 

Unable to switch between python versions without execution

3. Install Python 3.7. 5 with pyenv

1) Check the versions that can be installed (only the version number is the official version, and the others are derivative versions)


pyenv install --list

2) Install the required version


pyenv install -v 3.7.5

3) Update the database


pyenv rehash

4) View the installed version of the system


pyenv versions

The * indicates the version of the system that is currently in use

5) Switch python version


pyenv local 3.7.5
python -V
Python 3.7.5

pyenv local system
python -V
Python 2.7.16

6) Current python version


python --version

7) Try restarting the terminal

4. Solve the problem of slow installation speed of Python with pip command, and use domestic mirror image instead


#  It's best to update it before installing it. It hasn't been updated for too long homebrew May not be able to install 
brew update
#  Installation pyenv At present, the latest 2.x
brew install pyenv
0

5. The switching version is invalid, try bash


#  It's best to update it before installing it. It hasn't been updated for too long homebrew May not be able to install 
brew update
#  Installation pyenv At present, the latest 2.x
brew install pyenv
1

Attention

If homebrew is too slow, you can switch to domestic source 2 or install v2ray, copy the terminal agent command, paste it to the command line, and the command line can surf the Internet scientifically. Since the mac terminal defaults to zsh, if you are used to bash, refer to the above bash settings, which are actually 1. zsh switching version is effective. You can try bash, and Grandma can test it

Reference

Combing the configuration methods of environmental variables under Linux (the difference between. bash_profile and. bashrc): https://www.ofstack.com/article/184282. htm
Replace Homebrew image source under Mac: https://www.ofstack.com/article/217216. htm

How to switch Python version under windows: https://www.cnblogs.com/modentime/p/12807191. html
homebrew Tsinghua Source: https://mirrors.tuna.tsinghua.edu.cn/


Related articles: