【Python】pip 使用

Posted by 西维蜀黍 on 2021-05-04, Last Modified on 2022-04-10

Install pip

Install pip for Python 3

Ubuntu

Ubuntu 18.04 ships with Python 3, as the default Python installation. Complete the following steps to install pip (pip3) for Python 3:

Start by updating the package list using the following command:

$ sudo apt update

Use the following command to install pip for Python 3:

$ sudo apt install python3-pip

The command above will also install all the dependencies required for building Python modules.

Once the installation is complete, verify the installation by checking the pip version:

$ pip3 --version

The version number may vary, but it will look something like this:

$ pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

macOS

First of all download the get-pip file

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

Now run this file to install pip

$ python get-pip.py

Upgrade pip

Linux 或 macOS

pip install --upgrade pip    # python2.x
pip3 install --upgrade pip   # python3.x

Windows 平台升级:

python -m pip install -U pip   # python2.x
python -m pip3 install -U pip    # python3.x

Package Source Setting

Refer to https://pip.pypa.io/en/stable/cli/pip_config/

Check pip version

$ pip --version
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)

You can make sure that pip is up-to-date by running:

$ py -m pip install --upgrade pip

pip Commands

Install Software

Let’s say you want to install a package called scrapy which is used for scraping and extracting data from websites.

To install the latest version of the package you need to run the following command:

$ pip3 install scrapy

To install a specific version of the package you would issue the following command:

$ pip3 install scrapy==1.5

Replace pip3 with pip if using Python 2.

Specify repo, i.e., from a specified source

$ pip install -i https://pypi.xxx.com/ xxx-cli
$ pip3 install homeassistant -i https://pypi.python.org/simple

Upgrading packages

pip can upgrade packages in-place using the --upgrade flag. For example, to install the latest version of requests and all of its dependencies:

$ pip install --upgrade requests

Listing Installed Packages

To list all the installed pip packages use the command below:

$ pip3 list

Upgrade a Package With Pip

To upgrade an installed package to the latest version, run:

$ pip3 install --upgrade package_name

Uninstalling Packages With Pip

To uninstall a package run:

$ pip3 uninstall package_name

Show Denpendencies

Pip can export a list of all installed packages and their versions using the freeze command:

$ pip freeze

Which will output a list of package specifiers such as:

cachetools==2.0.1
certifi==2017.7.27.1
chardet==3.0.4
google-auth==1.1.1
idna==2.6
pyasn1==0.3.6
pyasn1-modules==0.1.4
requests==2.18.4
rsa==3.4.2
six==1.11.0
urllib3==1.22

pip Config File

Per-user:

  • On Unix the default configuration file is: $HOME/.config/pip/pip.conf which respects the XDG_CONFIG_HOME environment variable.
  • On macOS the configuration file is $HOME/Library/Application Support/pip/pip.conf if directory $HOME/Library/Application Support/pip exists else $HOME/.config/pip/pip.conf.
  • On Windows the configuration file is %APPDATA%\pip\pip.ini.

There is also a legacy per-user configuration file which is also respected. To find its location:

  • On Unix and macOS the configuration file is: $HOME/.pip/pip.conf
  • On Windows the configuration file is: %HOME%\pip\pip.ini
# locate config file
$ pip config list -v
For variant 'global', will try loading '/Library/Application Support/pip/pip.conf'
For variant 'user', will try loading '/Users/shiwei/.pip/pip.conf'
For variant 'user', will try loading '/Users/shiwei/.config/pip/pip.conf'
For variant 'site', will try loading '/System/Library/Frameworks/Python.framework/Versions/2.7/pip.conf'
[global]
timeout = 60
index-url = https://nexus3.onap.org/repository/PyPi/simple

See https://pip.pypa.io/en/stable/user_guide/#config-file

Reference