Article directory
Use Homebrew domestic source installation (super simple)
If you are not happy to read the detailed introduction above, just enter the following command directly in the terminal:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
According to the nanny-level tutorial notes in the terminal execution process, it can be installed soon, without additional operations such as manually modifying the "source" .
使用"brew doctor"命令来验证是否安装成功;
如果未提示command not found,则表示安装成功;
出现warning不要管。
Install Python3 using homebrew
install python3
Use the brew search python3 command to view the version of python3 that can be installed
Usebrew install python3The command installs the highest version of python3 by default, here is version 3.9.
Use after installationpython3 --versionCommand to view the specific version installed, here is version 3.9.12.
Modify environment variables
Currently inputting "python" in the terminal will still use the python2.7 that comes with the system. Next, modify the environment variable so that the input "python" opens the python3.9 just installed
Enter environment variables in the ".bash_profile" file
If the environment variable is configured for the first time, use " touch .bash_profile
" to create a hidden configuration file of .bash_profile.
If the configuration file .bash_profile already exists, use the " open .bash_profile
" command directly to open the configuration file.
Enter the following three lines in the ".bash_profile" file and save it:
export PATH=${PATH}:/usr/local/Cellar/[email protected]/3.9.12/bin
alias python="/usr/local/Cellar/[email protected]/3.9.12/bin/python3"
alias pip="/usr/local/Cellar/[email protected]/3.9.12/bin/pip3"
Note:
The above three lines should be modified according to your own path. My method of determining the path is as follows:
ls /usr/local/Cellar
ls /usr/local/Cellar/[email protected]
ls /usr/local/Cellar/[email protected]/3.9.12
ls /usr/local/Cellar/[email protected]/3.9.12/bin
The execution result is shown in the red box in the figure below:
After saving, execute it on the terminal, activate the configuration file to make the environment variables take effect, and enter in the terminal:
source ~/.bash_profile
At this time, enter python and display python3.9, as shown in the figure below:
The solution to the failure of environment variables after restarting the terminal
After closing and restarting the terminal, possibly typing "python", it still shows python2.7. This is caused by some macOS systems.
Create a new file named ".zshrc", the command is touch ~/.zshrc
;
Use to open ~/.zshrc
open the file, enter source ~/.bash_profile
, save and exit the file.
Type in terminal to source ~/.zshrc
activate.
After entering "python" in the terminal, python3 will be opened.