Beginners in Python know that the Mac comes with Python 2 7, but we may use Python 3. *, Therefore, multi version management of Python is very important. This paper will use pyenv for multi version management of Python.
1. Install pyenv
First, we need to install brew. If not, the installation method is as follows:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
To install pyenv, you can use brew:
brew update brew install pyenv
To configure pyenv environment variables:
If you use the built-in shell tool, execute the following in Terminal:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrc exec $SHELL -l
If an individual has zshell installed, execute the following at Terminal:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init -)"' >> ~/.zshrc exec $SHELL -l
How to check whether it is a built-in shell or a zshell? You can know by executing the following command:
echo $SHELL
2. Install python
Execute the command to view the installable python version:
pyenv install --list
Among them, those with only version number such as x.x.x are the official version of python, while others with both name and version such as xxxxx-x.x.x belong to "derivative version" or distribution version. With the latest Python 3 9.0 as an example:
pyenv install 3.9.0 -v
However, python-3.9.0 will be downloaded tar. XZ package, as shown in the figure, downloading is very slow:
$ pyenv install 3.9.0 -v python-build: use openssl@1.1 from homebrew python-build: use readline from homebrew /var/folders/qj/7g6cxp596b93yqm5v8z3k05c0000gn/T/python-build.20201112095616.3069 /usr/bin Downloading Python-3.9.0.tar.xz... -> https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz
So we have two ways:
- Replace with domestic image library;
- Download Python packages manually.
We download the package manually: Python official download address , after downloading, put it in the following path:
~/.pyenv/cache
If you do not have this path, you can create it directly (~ /. Pyenv because pyenv must exist, but there may not be a cache folder):
mkdir -p ~/.pyenv/cache
After installation, you can find that the download process is skipped directly:
$ pyenv install 3.9.0 -v python-build: use openssl@1.1 from homebrew python-build: use readline from homebrew /var/folders/qj/7g6cxp596b93yqm5v8z3k05c0000gn/T/python-build.20201112095950.3796 /usr/bin /var/folders/qj/7g6cxp596b93yqm5v8z3k05c0000gn/T/python-build.20201112095950.3796/Python-3.9.0 /var/folders/qj/7g6cxp596b93yqm5v8z3k05c0000gn/T/python-build.20201112095950.3796 /usr/bin Installing Python-3.9.0... python-build: use readline from homebrew
Uninstallation is also simple:
pyenv uninstall 3.9.0 -v
3. python version management
After installation, the database needs to be updated:
$ pyenv rehash
View the currently installed python Version (if pyenv version is executed, the current version will be displayed directly):
$ pyenv versions * system (set by /home/seisman/.pyenv/version) 3.9.0
The asterisk indicates that you are currently using the system's own python.
Set the Python version of the global:
$ pyenv global 3.9.0 $ pyenv versions system * 3.9.0 (set by /home/seisman/.pyenv/version)
Confirm the python version and directly execute the command Python:
$ python Python 3.9.0 (default, Nov 12 2020, 10:01:22) [Clang 12.0.0 (clang-1200.0.32.21)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> exit()
You can execute python global system again, and then switch back to the built-in version of the system.
In addition, there are three forms of python switching: global, local and shell:
// Different python Switch between versions // Set global Python Version, by writing the version number to ~/.pyenv/version Document mode pyenv global 2.7.16 // set up Python Local version, by writing the version number to the .python-version File mode. Set in this way Python Version priority global high pyenv local 2.7.16 // Set the current session of the Shell. The wisdom will affect the Python version of the current session. Once the current Terminal is turned off or another Terminal is opened, it will be invalid pyenv shell 2.7.16 // Priority: shell > local > Global
Note:
- The system's own script will directly call the old version of Python in the form of / usr/bin/python, so it will not affect the system script;
- When using pip to install the third-party module, it will be installed to ~ / pyenv/versions/3.4.1 will not conflict with the system module.
- After using pip to install the module, you may need to execute pyenv rehash to update the database;
4. Python installation path problem
We can know where the Python we installed is located in this way:.
$ python WARNING: Python 2.7 is not recommended. This version is included in macOS for compatibility with legacy software. Future versions of macOS will not include Python 2.7. Instead, it is recommended that you transition to using 'python3' from within Terminal. Python 2.7.16 (default, Jun 5 2020, 22:59:21) [GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> print(sys.path) ['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload'
, '/Library/Python/2.7/site-packages'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python'
, '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC'] >>>
From this, we can know the path of our installation. Some netizens always cut as follows:
- The python path of Mac system is / system / library / frameworks / python framework/Version. There may be multiple Python versions here. Current stores the current Python version of the system. Enter Current/bin and enter it in the terminal/ python --version to view the current Python version of the system (Note: if you use the python --version command to view the current Python version of the user rather than the system Python version)
- The HomeBrew installation path is / usr / local / cell / python, where the installed version of HomeBrew is stored. Enter 2.7.16/bin and enter it on the terminal/ python --version to view the python version currently used by the user. If you use brew to install Python correctly, the user's current Python version will be the newly installed python.
- The default path for system commands is / usr/bin, and the default path for user commands is / usr/local/bin (the default path for brew installation). If there are the same commands, they will be searched in turn according to the order of environment variables in the / etc/paths file (the front takes precedence over the back). You can also enter echo $PATH at the terminal to view the environment variables. Follow the path on the left takes precedence over the path on the right.
5. Install python from CommandLineTools for Mac
One day, I suddenly found a python 3 in my mac 8.5. I'm curious about how it came from. According to the method in Chapter 4 above, I see that it's the following commandLineTools:
'/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python38.zip', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/lib-dynload', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages'
Python3 in the directory of / Library/Developer/CommandLineTools should be generated when a certain version of Xcode command line tools is installed, but the new version of Xcode command line tools does not contain python3, so you can uninstall Xcode command line tools first. According to Apple's official documents, this directory can be deleted directly:
sudo rm -rf /Library/Developer/CommandLineTools sudo rm -f /usr/bin/python3
However, / usr/bin/python3 cannot be deleted casually. Previously, su root was used to switch to the root user (the display # number after switching indicates that the switching is successful), and then execute the following command to delete it.
chmod 777 /usr/bin/python3
However, after the macbook pro was upgraded, it could not be completed directly because it was already read only. In order to protect the system and prevent malicious operations, it is not even allowed to create a folder in the root directory (there is no option to create a new folder):
However, this can be handled as follows:
Shut down press Command+R to restart, enter recovery mode, shut down SIP: open the Terminal gadget Terminal, and enter the command:
csrutil disable
After normal restart, re mount the root directory: enter the command
sudo mount -uw /
At this point, you can delete the folder just now. At the same time, you can also create a folder in your root directory:
Create a data directory where you like, such as:
mkdir /Users/xxx/data
Then establish a soft connection:
sudo ln -s /Users/xxx/data /data
Shut down, press Command+R to restart, enter the recovery mode, reopen SIP: enter the command at the terminal
csrutil enable
The / data directory can be restarted normally.
After deletion, reinstall Xcode command line tools:
xcode-select --install
If the prompt fails to install successfully, don't worry. You can manually download it from Apple's official website and install it.
The download address is: https://developer.apple.com/download/more/ , search Command Line Tools, and then select a download to install.