Mac python安装pip

Before we start with how to install pip for Python on macOS, let’s first go through the basic introduction to Python. 

Python is a widely-used general-purpose, high-level programming language. Python is a programming language that lets you work quickly and integrate systems more efficiently. PIP is a package management system used to install and manage software packages/libraries written in Python. These files are stored in a large “on-line repository” termed as Python Package Index (PyPI). pip uses PyPI as the default source for packages and their dependencies. In this article, we will see How to Install PIP on a Mac. Using pip you can install any package using the following syntax:

Syntax of pip

pip install package_name

Download and Install Pip on macOS

pip can be downloaded and installed usingthe  command line by going through the following steps:

Step 1: Download the  get-pip.py(https://bootstrap.pypa.io/get-pip.py) file and store it in the same directory as python is installed. or Use the following command to download pip directly

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Step 2: Now execute the downloaded file using the below command

python3 get-pip.py

Step 3:  Wait through the installation process. 

Mac python安装pip

 

Voila! pip is now installed on your system.

Verification of the Installation process

One can easily verify if the pip has been installed correctly by performing a version check on the same. Just go to the command line and execute the following command:

pip3 --version
pip --version

Mac python安装pip

 

How to Update PIP on a Mac

You can use the following command in your terminal to upgrade your pip. 

python3 -m pip install –upgrade pip

How to install a specific version of pip

To install a specific version you can enter the following version number to install the specific version.

Installing a separate copy of Python is a popular option, even though Python already comes with macOS. You take on the responsibility to make sure you're using the copy of Python you intend. But, the benefits are having the latest Python release and some protection from hosing your system if things go badly wrong.

To install Python using HomeBrew:

brew update
brew install python # or brew install python3

Now confirm that we're working with our newly installed Python:

ls -lh `which python`

...should show a symbolic link to a path with "Cellar" in it like:

lrwxr-xr-x  1 chris  admin    35B Dec  2 13:40 /usr/local/bin/python -> ../Cellar/python/2.7.8_2/bin/python

Pip should be installed along with Python. You might want to upgrade it by typing:

pip install --upgrade pip

Now you're ready to install any of the 50,000+ packages on PyPI.

Other Notes

Formerly, I've used get-pip.py to install pip. But, the docs warn that get-pip.py does not coordinate with package managers and may leave your system in an inconsistent state. Anyway, there's no need, given that pip is now included with Python as of 2.7.9.

Note that pip isn't the only package manager for Python. There's also easy_install. It's no good to mix the two, so don't do it.

Finally, if you have both Python 2 and 3 installed, pip will point to whichever Python you installed last. Get in the habit of explicitly using either pip2 or pip3, so you're sure which Python is getting the new library.

总结一下自己在安装python时候遇到的问题

环境说明 macOS Catalina 版本10.15.7

安装python

廖大大的python教程里说明如下:

如果你正在使用Mac,系统是OS X>=10.9,那么系统自带的Python版本是2.7。要安装最新的Python 3.8,有两个方法:

方法一:从Python官网下载Python 3.8的安装程序,下载后双击运行并安装;

方法二:如果安装了Homebrew,直接通过命令brew install python3安装即可。

  • 问题1. 我使用方法2安装之后,使用python --version命令之后,发现结果还是2.7.x。这是咋回事?大佬能写错?

后面发现如果使用python命令,是默认的python2.7.x。如果我们要查看其他版本,比如python3,那么我们要使用python3 --version

  • 另外,如果官网下载安装结束以后,如果像我只是想使用python的命令,是不需要打开对应的程序,只需要在终端输入python3 xxx即可

  • 目前brew安装的是3.8.2版本

  • 问题3. sudo easy_install pip报错File "/tmp/easy_install-1BTu9S/pip-21.3/setup.py", line 7 def read(rel_path: str) -> str: ^ SyntaxError: invalid syntax

  • 如果需要卸载2.7.x版本,只需执行brew uninstall python即可

安装pip

官方地址

需要先downloadget-pip.py这个文件,可以使用wget或者直接打开地址,复制到编辑器中保存并命名为get-pip.py

该步骤中要使用命令sudo python3 get-pip.py即可(注意是python3,因为该命令要求的python版本为3.6?The minimum supported Python version is 3.6)

  • 问题,如果出现WARNING: The scripts pip, pip3 and pip3.8 are installed in '/Library/Frameworks/Python.framework/Versions/3.8/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

解决方案将python3 --version0添加到python3 --version1文件中

我在看到别人对该问题的解答的时候不明觉厉,这个文件是啥?在哪里?

这篇文章写的很详细Mac 打开、编辑 .bash_profile 文件

总结

  1. 首先需要参考的一定是官方文档,别人总结的内容,我们不一定能发现原因及问题
  2. 问题是一个个解决的,不要着急,一个方案它没有成功,可能因为我们只是触及到表面,有些其他环境问题或者之类的,我们这边不齐全