Mac environment Python2.7 upgraded to Python3.6

Original link: https://blog.csdn.net/xummgg/article/details/69053334

1 Introduction

Mac system comes with python2.7, the purpose of this article is to upgrade the built-in python to version 3.6.
There are many methods on the Internet that allow the two versions of python2.7 and python3.X to coexist. The blogger does not know whether it is better for the two versions to coexist or to upgrade directly, so readers should choose the method carefully.

2. Turn off the Rootless mechanism

Because python2.7 under Mac is installed in the /System directory by default. But ~~~Mac has a Rootless mechanism, which does not allow modification directly under /System by default. Therefore, the Rootless mechanism must be turned off first.

How to turn off the Rootless mechanism:
Turn off:
1). Restart the computer, press and hold command+R during the restart process to enter the recovery mode
2). Open the terminal and type: csrutil disable
3). Restart the computer

If you want to turn on the Rootless mechanism later, the method is as follows:
Turn on:
1). Restart the computer, press and hold command+R during the restart process to enter the recovery mode
2). Open the terminal and type: csrutil enable
3). Restart the computer

3. Download and install python3.6

Download the pkg version from the official website https://www.python.org/downloads/
and install it. Select the default path for installation, and it will be installed under the /Library/Frameworks/Python.framework/Versions/ directory

4. Delete python2.7

sudo rm -R /System/Library/Frameworks/Python.framework/Versions/2.7

5. Mobile python3.6

Install python3.6 to the /System/Library/Frameworks/Python.framework/Versions/ directory:

sudo mv /Library/Frameworks/Python.framework/Versions/3.6 /System/Library/Frameworks/Python.framework/Versions

6. Modify the Group to which the file belongs

Set Group to wheel, which is what the original system comes with.

sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/3.6

7. Update Current Link

There is a Current link in the Versions directory, which points to the current Python version. The original point is to the Python2.7 that comes with the system. After we delete it, the link becomes invalid, so we need to relink it.

sudo rm /System/Library/Frameworks/Python.framework/Versions/Current
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6 /System/Library/Frameworks/Python.framework/Versions/Current

8. Relink executable files

  1. Delete the original executable file of the system first
sudo rm /usr/bin/pydoc
sudo rm /usr/bin/python
sudo rm /usr/bin/pythonw
sudo rm /usr/bin/python-config
  1. Create a new link
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/pydoc3.6 /usr/bin/pydoc
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /usr/bin/python
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/pythonw3.6 /usr/bin/pythonw
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config /usr/bin/python-config

9. Update the .bash_profile file

The python bin in the default bash_profile points to /Library/Frameworks/Python.framework/Versions/3.6/bin. To change to the /System/ directory

vim ~/.bash_profile (只要能编辑就行)插入新的Python路径

# Setting PATH for Python 3.6

# The orginal version is saved in .bash_profile.pysave
PATH="/System/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

I don't have a .bash_profile file by default, so I just create it myself.

10. Uninstall python3.6 installed by pkg

If this step is not done, errors will still occur when using the pip3 command (it connects to the /Library/ directory and follow the pip3 command by default, but in fact it should be found in the /System/Library/ directory). The blogger has been in this pit for a long time.

I use the CleanApp software to uninstall the python3.6 originally installed by pkg, and uninstall both of the installed software.

11. Testing

In the command line, use pip -V and pip3 -V to view the version and location. Enter with python.

Guess you like

Origin blog.csdn.net/u010670117/article/details/89003318
Recommended