All notes
Pip

pip

What is pip's full name? I don't find any definition online, but this "The PyPA recommended tool for installing and managing Python packages." from pip website.(PyPA: Python Parser) So pip is just a command line tool to manage python packages, which is to say, it is not a python command! And you can't run it directly from python!

On windows, to use pip, you need to add the path: Python2/Scripts (pip.exe resides in it).

StackOverflow: download-only.


# Upgrade pip itself. It also works in windows cmd.
pip install -U pip
# Windows version
python -m pip install -U pip

pip install SomePackage==1.0.4     # specific version
pip install 'SomePackage>=1.0.4'     # minimum version

pip uninstall somePackage

pip list
pip list --outdated
pip show somePackege
pip search "query"

# -d, --download
pip install -d /somePath package

# Install offline.
# --no-index: Ignore package index (only looking at --find-links URLs instead).
# -f, --find-links url: If a url or path to an html file, then parse for links to archives. If a local path or file:// url that's a directory,then look for archives in the directory listing.
pip install --no-index -f /somePath package

pip install ./downloads/SomeProject-1.0.4.tar.gz

# Or install from the requirement
# -r, --requirement
pip install -d /somePath -r requirementFile

Requirements.txt


pip freeze
# docutils==0.11
# Jinja2==2.7.2
# MarkupSafe==0.19
# Pygments==1.6
# Sphinx==1.2.2

# Generate a requirements file:
pip freeze > requirements.txt

# Install from it:
pip install -r requirements.txt

pip.conf

pip.pypa.io: user guide.

File location

If multiple configuration files are found by pip then they are combined in the following order:

  1. Firstly the site-wide file is read, then
  2. The per-user file is read, and finally
  3. The virtualenv-specific file is read.

Each file read overrides any values read from previous files, so if the global timeout is specified in both the site-wide file and the per-user file then the latter value is the one that will be used.

Syntax

The names of the settings are derived from the long command line option, e.g. if you want to use a different package index (--index-url) and set the HTTP timeout (--default-timeout) to 60 seconds your config file would look like this:


[global]
timeout = 60
index-url = http://download.zope.org/ppix

Decreasing the timeout to 10 seconds when running the freeze (Freezing Requirements) command and using 60 seconds for all other commands is possible with:


[global]
timeout = 60
[freeze]
timeout = 10

Boolean options like --ignore-installed or --no-dependencies can be set like this:


[install]
ignore-installed = true
no-dependencies = yes

To enable the boolean options --no-compile and --no-cache-dir, falsy values have to be used:


[global]
no-cache-dir = false

[install]
no-compile = no

Appending options like --find-links can be written on multiple lines:


[global]
find-links =
    http://download.example.com

[install]
find-links =
    http://mirror1.example.com
    http://mirror2.example.com

Change pypi host

CSDN.


pip install -i http://mirrorURL/simple packageName --trust-host mirrorURL
pip install -i http://pypi.douban.com/simple django --trusted-host pypi.douban.com
pypi.python.org
pypi.douban.com
pypi.hustunique.com

To permanently add the customized pypi host, add in ~/.config/pip/pip.conf:


[global]
timeout = 10
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com

Environment Variables

pip's command line options can be set with environment variables using the format PIP_"UPPER_LONG_NAME" . Dashes (-) have to be replaced with underscores (_). For example, to set the default timeout:


export PIP_DEFAULT_TIMEOUT=60

## This is the same as passing the option to pip directly:
pip --default-timeout=60 [...]

To set options that can be set multiple times on the command line, just add spaces in between values. For example:


export PIP_FIND_LINKS="http://mirror1.example.com http://mirror2.example.com"

# Is the same as calling:
pip install --find-links=http://mirror1.example.com --find-links=http://mirror2.example.com

Config Precedence

Precedence from high to low:

  1. Command line options
  2. Environment variables
  3. The config file. Within the config file, command specific sections have precedence over the [global] section.

Examples:

--host=foo overrides PIP_HOST=foo
PIP_HOST=foo overrides a config file with [global] host = foo

Command Completion

pip comes with support for command line completion in bash and zsh.


# To setup for bash:
pip completion --bash >> ~/.profile

# To setup for zsh:
pip completion --zsh >> ~/.zprofile

Alternatively, you can use the result of the completion command directly with the eval function of your shell, e.g. by adding the following to your startup file:


eval "`pip completion --bash`"

Installing from local packages


# Download the archives that fulfill your requirements:
pip install --download DIR -r requirements.txt

# If some of your requirements don't come as wheels from PyPI, and you want wheels, then run this instead:
pip wheel --wheel-dir DIR -r requirements.txt

# To install from local only:
pip install --no-index --find-links=DIR -r requirements.txt

pip wheel

readTheDocs: pip wheel.

Wheel is a built-package format, and offers the advantage of not recompiling your software during every install.

'pip wheel' uses the bdist_wheel setuptools extension from the wheel package to build individual wheels.


# Build wheels for a requirement (and all its dependencies), and then install
# -w, --wheel-dir
pip wheel --wheel-dir=/tmp/wheelhouse SomePackage
pip install --no-index --find-links=/tmp/wheelhouse SomePackage

PyPI mirrors and caches

ReadTheDocs: mirrors.