pip 安装和使用

一、手动安装

对于 python 3.4 + 默认带有pip,可以尝试引导它

python -m ensurepip –default-pip

通用安装方式

获取安装代码
https://bootstrap.pypa.io/get-pip.py

python get-pip.py
默认会安装 setuptools 和 wheel
可通过下面参数取消安装:
–no-setuptools
–no-wheel

安装选项还可以使用 pip command [options] 和 pip install [options] 的选项

pip command [options]
https://pip.pypa.io/en/latest/reference/pip/#general-options
pip install [options]
https://pip.pypa.io/en/latest/reference/pip_install/#pip-install-options

从指定位置安装包
python get-pip.py –no-index –find-links=/local/copies

安装到 user site:
python get-pip.py –user

指定代理:
python get-pip.py –proxy=”http://[user:passwd@]proxy.server:port”

安装包安装方式

进入 pypi 官网下载源码包
https://pypi.python.org/pypi/pip#downloads

解压源码包 python setup.py install

Ubuntu 下安装

apt-get install python-pip
apt-get install python3-pip

pip 升级

Linux: pip install -U pip
Windows: python -m pip install -U pip

如果安装了 setuptools、wheel,也要一起升级:

pip install -U pip setuptools wheel
python -m pip install –upgrade pip setuptools wheel

二、pip 和 pip3

主要看环境变量的设置,python3 中 有时为了方便,也会命名 pip

三、更换国内 pypi 源

Linux系统

mkdir ~/.pip

cat > ~/.pip/pip.conf << EOF

[global]
trusted-host=mirrors.aliyun.com
index-url=https://mirrors.aliyun.com/pypi/simple/

EOF

Windows系统

首先在window的文件夹窗口输入 : %APPDATA%

然后创建pip文件夹

最后创建pip.ini文件,写入如下内容

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

四、pip 使用

运行

pip < command> [options] # 基本格式
python -m pip < command> [options] # 指定解释器运行

安装

pip install SomePackage # 最新版本
pip install SomePackage==1.0.4 # 特定版本
pip install ‘SomePackage>=1.0.4’ # 最小版本
pip install SomePackage>=1.0.4,<=2.0 #

pip install -r requirements.txt # 从文件安装

####### example-requirements.txt #######
#
###### Requirements without Version Specifiers ######
nose
nose-cov
beautifulsoup4
#
###### Requirements with Version Specifiers ######
#  See https://www.python.org/dev/peps/pep-0440/#version-specifiers
docopt == 0.6.1            # Version Matching. Must be version 0.6.1
keyring >= 4.1.1            # Minimum version 4.1.1
coverage != 3.5            # Version Exclusion. Anything except version 3.5
Mopidy-Dirble ~= 1.1        # Compatible release. Same as >= 1.1, == 1.*
#
###### Refer to other requirements files ######
-r other-requirements.txt
#
#
###### A particular file ######
./downloads/numpy-1.9.2-cp34-none-win32.whl
http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl
#
###### Additional Requirements without Version Specifiers ######
#  Same as 1st section, just here to show that you can put things in any order.
rejected
green
#
###### Other arguments ######
--no-index
--index-url https://pypi.python.org/simple/
--find-links /my/local/archives
--find-links http://some.archives.com/archives

pip install –upgrade SomePackage # 升级安装

pip install SomePackage-1.0-py2.py3-none-any.whl # 指定 whl 文件安装
pip install –no-index –find-links=/local/wheels -r requirements.txt # 只使用本地 whl 安装需要的包

pip install ./downloads/SomePackage-1.0.4.tar.gz # 指定源码安装
pip install http://my.package.repo/SomePackage-1.0.4.zip

pip install –user # 安装到 python 用户站点包目录,
# 默认 Linux ~/.local,MacOS ~/Library/Python/X.Y,Windows %APPDATA%\Python,
# 可通过 PYTHONUSERBASE 变量配置
# 当与系统安装包冲突时,忽略系统安装包;
# 当系统安装包满足要求时,不进行安装;
# 不在 virtualenv 上安装,对于 –system-site-packages 的 virtualenv 当有冲突时,也不安装。

pip install -e . # 安装当前目录已打包好的项目
pip install -e path/to/project # 安装指定目录已打包好的项目

pip install -e git+https://git.repo/some_pkg.git#egg=SomePackage # from git
pip install -e hg+https://hg.repo/some_pkg.git#egg=SomePackage # from mercurial
pip install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomePackage # from svn
pip install -e git+https://git.repo/some_pkg.git@feature#egg=SomePackage # from ‘feature’ branch
pip install -e “git+https://git.repo/some_repo.git#egg=subdir&subdirectory=subdir_path” # install a python package from a repo subdirectory

pip install –pre SomePackage # 安装预发行和安装的版本,默认安装的是稳定版本

卸载

pip uninstall -y SomePackage
pip uninstall -r requirements.txt

查看

pip list  # 列举所有包
pip list –outdated/-o # 列举可升级的包
pip list –format=json # json 格式输出
pip list –format=freeze # freeze 格式输出

pip show SomePackage # 查看包的信息
pip show –verbose SomePackage # 查看包的所有信息
pip show -f SomePackage # 显示包所在的目录

搜索

pip search < query >

其它

pip freeze > requirements.txt

pip wheel –wheel-dir=/local/wheels SomePackage # 打包成 whl 文件
pip wheel –wheel-dir=/local/wheels -r requirements.txt

pip install –download DIR SomePackage # 包下载到指定目录
pip install -d DIR -r requirements.txt

五、配置文件

全局配置

Linux
/etc/pip.conf
也遵循 XDG_CONFIG_DIRS 变量

MacOS
/Library/Application Support/pip/pip.conf

WinXp
C:\Documents and Settings\All Users\Application Data\pip\pip.ini
Win7 +
C:\ProgramData\pip\pip.ini
不支持 WinVista

基于用户

Linux
$HOME/.config/pip/pip.conf
也遵循 XDG_CONFIG_HOME 变量

MacOS
HOME/Library/ApplicationSupport/pipexists H O M E / L i b r a r y / A p p l i c a t i o n S u p p o r t / p i p e x i s t s 否 则 是 HOME/.config/pip/pip.conf

Windows
%APPDATA%\pip\pip.ini

virtualenv 虚拟环境中

Linux 或 MacOS
$VIRTUAL_ENV/pip.conf

Windows
%VIRTUAL_ENV%\pip.ini

配置格式

[global]
timeout = 60
index-url = http://download.zope.org/ppix
no-cache-dir = false
find-links =
 http://download.example.com

[freeze]
timeout = 10

[install]
ignore-installed = true
no-dependencies = yes
no-compile = no
find-links =
 http://mirror1.example.com
 http://mirror2.example.com

环境变量

PIP_< UPPER_LONG_NAME>
将参数名中 - 替换为 _ 并大写

export PIP_FIND_LINKS=”http://mirror1.example.com http://mirror2.example.com”
等同于
pip install –find-links=http://mirror1.example.com –find-links=http://mirror2.example.com

你可能感兴趣的:(Python)