Install and Control Different Version of Python on Mac
The default version of Python installed on the Mac OS is 2.7, and if we intall Anaconda, the version 3.7 of Python would overwrite the version 2.7 to become the default version of Python in the computer. But now some third-party modules might require the Python version to be 3.8 at least (It's the situation I met when I try to use snscrape, a module for social media data crawler). Under this situation, we need to download and install the right version of Python. When different version of Python exist on the system, we need to arrange them properly to avoid some potential issues.
(1) Download the installation package from the official website and install it on Mac
Choose macOS 64-bit installer, and doucble click the installer file to install after it's downloaded. Pay attention to the installation path, the default path is /Library/Frameworks/Python.framework/Versions/3.8
(2) Modify environment variables
In order to make the newly installed Python work, we have to add its installation path to environment variables. Run vim ~/.bash_profile on terminal, and then add the following two codes to bash_profile.
1 |
|
These are to repoint the python command to the newly installed version. After the editing above, run source ~/.bash_profile on terminal to make the execution command above take effect. Then we can run python -V on terminal to check if the current version matches with the newly installed version.
(3) Install pip
If we install multiple versions of Python, we also need to install different pip for each version of Python. To install new pip, run the following commands on terminal.
1 |
|
Every time pip is installed, it will modify the Python version corresponding to the default pip. Then we should run the following code on terminal to check which pip is corresponding with which Python
1 |
|
(4) Manage different version of Python to use which pip
Generally the pip would be corresponding to the Python that is set in the environment variable, if we want to make other Python to use pip, we need to adjust the pip file.
1 |
|
Here the default Python is version 3.6, we can change the first line into other Python installation path, then this pip will be used by the Python we just set. If we want to make the specific version of Python use pip2, pip3, ..., pipn, we just need to set the first line of these pip files to the installation path of the Python we want.
All articles in this blog adopt the CC BY-SA 4.0 agreement except for special statements. Please indicate the source for reprinting!