How to install Python3 and PIP on MACOS

foreword

Recently, because I need to execute some python scripts on MACOS, I installed a MACOS virtual machine. However, the default python version installed on MAC is 2.7.10, and the script is written based on python3. In order to execute the script smoothly, I started to upgrade the python version.

1. Download the installation package

Go to the python official website https://www.python.org/ , find the corresponding version package and download it.
insert image description here

2. python installation

2.1 Find the downloaded installation package, double-click to install:

insert image description here

2.2 Install step by step according to the guide:

insert image description here
insert image description here
During the period, there will be operations such as confirming the agreement, entering the password, etc., just follow the prompts.

2.3 Wait for the installation to complete

insert image description here

2.4 After the installation is complete:

insert image description here

3 Verify installation

python -VExecute and respectively, python3 -Vyou can see that there are currently 2 versions of python:
insert image description here

4 Modify the default Python version

Now the default python version is still 2.7.10, you need to modify the environment variable to change the default version to the newly installed version 3.11.2.

4.1 View the path of python3:

Execute which python3the path to view python3:
insert image description here

4.2 Modify the .bash_profile file:

Open the terminal, execute vim ~/.bash_profile, insert in the corpus alias python="/usr/local/bin/python3", and save:
insert image description here

4.3 Application environment variables:

Open the terminal and press source ~/.bash_profileEnter to make the configuration just written take effect. At this point, the input python -Vwill default to version 3.11:
insert image description here

5 install pip

In order to facilitate the management of python packages, install a pip here by the way

5.1 Get get-pip.py

Execute the command in the terminal curl 'https://bootstrap.pypa.io/get-pip.py' > get-pip.pyto get get-pip.py
insert image description here

5.2 Execute pip installation:

Execute the command to install pip in the terminal python get-pip.py:
insert image description here

5.3 Check pip version:

Execute in the terminal pip -Vto check the version of pip:
insert image description here

5.4 Use pip to install the required packages:

Execute in the terminal pip install 包名to install the required packages:
insert image description here

Guess you like

Origin blog.csdn.net/qq_17716819/article/details/129943424