Python pip usage guide

1. Query software package

Query all packages installed in the current environment

$ pip list

Query packages with a certain name on pypi

$ pip search pkg

Query the upgradeable packages in the current environment

$ pip list --outdated

Query the details of a package

$ pip show pkg

2. Download the software package

Download the package to the local without installing the package

$ pip download --destination-directory /local/wheels -r requirements.txt

Install package from directory

$ pip install --no-index --find-links=/local/wheels -r requirements.txt

Of course, you can also build the wheel file yourself

$ pip install wheel$ pip wheel --wheel-dir=/local/wheels -r requirements.txt

3. Install the package

Use  pip install <pkg> can be easily downloaded from the pypi search and install python package.

As follows

$ pip install requests

This is the basic format of the installation package, and we can add more parameters to it to achieve different effects.

3.1 Only install locally, not from pypi

# 已经下载了软件包$ pip install --no-index --find-links=/local/wheels pkg

3.2 Limited version for software package installation

The following three types restrict the version of a single python package

# 所安装的包的版本为 2.1.2$ pip install pkg==2.1.2
# 所安装的包必须大于等于 2.1.2$ pip install pkg>=2.1.2
# 所安装的包必须小于等于 2.1.2$ pip install pkg<=2.1.2

The following commands are used to manage/control the package version of the entire python environment

# 导出依赖包列表pip freeze >requirements.txt
# 从依赖包列表中安装pip install -r requirements.txt
# 确保当前环境软件包的版本(并不确保安装)pip install -c constraints.txt

3.3 Restriction to install without binary package

By default, the platform of the wheel package is the platform on which the pip download command is run, so the platform may not be compatible.

# 下载非二进制的包$ pip download --no-binary=:all: pkg
# 安装非二进制的包$ pip install pkg --no-binary

3.4 Specify the proxy server installation

$ pip install --proxy [user:passwd@]http_server_ip:port pkg

Configure the proxy:$HOME/.config/pip/pip.conf

For this path, a few points

• Different operating systems have different paths

# Linux/Unix:/etc/pip.conf~/.pip/pip.conf~/.config/pip/pip.conf
# Windows:
%APPDATA%\pip\pip.ini%HOME%\pip\pip.iniC:\Documents andSettings\All Users\Application Data\PyPA\pip\pip.conf (Windows XP)C:\ProgramData\PyPA\pip\pip.conf (Windows7及以后) 

• If you don’t have this file on your machine, just create it yourself

How to configure, here is an example:​​​​​​​

[global]index-url = http://mirrors.aliyun.com/pypi/simple/ 
# 替换出自己的代理地址,格式为[user:passwd@]proxy.server:portproxy=http://xxx.xxx.xxx.xxx:8080 
[install]# 信任阿里云的镜像源,否则会有警告trusted-host=mirrors.aliyun.com

3.5 Install user private software packages

Two methods:

1. Use a virtual environment

2. Install the package in the user's environment

The command is very simple, as long as you add  --user parameters, pip will install it under the current user  ~/.local/lib/python3.x/site-packages , and other users' python will not be affected.

pip install --user pkg

 

4. Uninstall the package

$ pip uninstall pkg

5. Upgrade package

$ pip install --upgrade pkg

When upgrading, there is actually an option --upgrade-strategythat is not used very much  , which is used to specify the upgrade strategy.

There are only two options:

eager : Upgrade all dependent packages • only-if-need: Only upgrade when the old version cannot adapt to the new parent dependent package.

After pip 10.0 version, the default value of this option is  only-if-need, so the following two writings are the same. ​​​​​​​

pip install --upgrade pkg1 pip install --upgrade pkg1 --upgrade-strategy only-if-need

Guess you like

Origin blog.csdn.net/lianshaohua/article/details/109199234