Python 第三方模块,通常以 Python 包方式进行发布。我们通过对这些包的管理,实现安装、卸载 Python 的第三方模块。
Python 包常采用源代码、egg、wheel 和 Windows 安装文件等格式进行发布。
Python 包源代码,常见有 tar.gz 格式(常用于 Linux 系统)和 zip 格式(常用于 Windows 系统)。Python 包的源代码包中包含 setup.py 文件,从源代码安装包,需要执行如下命令:
# python setup.py install
egg 格式是 setuptools 引入的一种文件格式,采用 .egg 扩展名,用于 Python 模块的安装。egg 文件实际上是 zip 压缩格式的源代码包。采用 egg 文件进行安装,执行如下命令:
# easy_install *.egg
wheel 本质上是一个 zip 包格式,它使用 .whl 扩展名,用于 python 模块的安装,它的出现是为了替代 egg。Wheel 规范请参见 PEP-0427 文档(http://www.python.org/dev/peps/pep-0427/)。
一些 Python 第三方模块,采用 C 或 Fortan 语言编写,采用源代码安装时,需要对源代码进行编译。Windows 系统中,由于源代码编译较为繁琐,所以许多 Python 第三方模块会发布 Windows 安装文件,常见有 exe 格式和 msi 格式。在 Windows 系统中,双击执行安装文件,根据安装向导完成安装操作。
Python 包管理常见的工具有:
推荐安装使用 setuptools 和 pip 工具。在 Linux 系统中(本文采用 CentOS 7.0 x64 系统),可以以管理员安装 pip 工具,该过程中会自动安装 setuptools 工具。
# wget https://bootstrap.pypa.io/get-pip.py # python get-pip.py
pip 工具安装完成后,会生成 /usr/bin/pip 可执行文件(命令)。对于日常的 Python 包管理(安装、卸载等)任务,使用 pip 工具即可。
pip 工具是推荐的 Python 包管理工具。pip 工具默认将 Python 包安装到“/usr/lib/python2.7/site-packages”目录中,使用 pip 工具需要管理员 root 权限。
$ pip --help Usage: pip <command> [options] Commands: install Install packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. search Search PyPI for packages. wheel Build wheels from your requirements. zip DEPRECATED. Zip individual packages. unzip DEPRECATED. Unzip individual packages. bundle DEPRECATED. Create pybundles. help Show help for commands. General Options: -h, --help Show help. -v, --verbose Give more output. Option is additive, and can be used up to 3 times. -V, --version Show version and exit. -q, --quiet Give less output. --log-file <path> Path to a verbose non-appending log, that only logs failures. This log is active by default at /home/centos/.pip/pip.log. --log <path> Path to a verbose appending log. This log is inactive by default. --proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port. --timeout <sec> Set the socket timeout (default 15 seconds). --exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup. --cert <path> Path to alternate CA bundle.
常见使用场景:
## 搜索包 # pip search dateutil ## 安装指定的包(通过使用 ==, >=, <=, >, < 来指定版本号) # pip install python-dateutil # pip install 'python-dateutil==2.2' ## 升级指定的包 # pip install -U python-dateutil # pip install --upgrade python-dateutil ## 查看已安装的包 # pip list ## 查看某个已安装的包 # pip show python-dateutil ## 卸载指定的包 # pip uninstall python-dateutil ## 以 requirements 格式输出已安装包的信息 # pip freeze
VirtualEnv 用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper 为前者提供了一些便利的命令行上的封装。
安装 virtualenv 和 virtualenvwrapper 环境:
# pip install virtualenv # pip install virtualenvwrapper
配置 virtualenvwrapper 环境:
# 在 ~./bash_profile 文件中加入如下两行 export WORKON_HOME=~/.virtualenvs source /bin/virtualenvwrapper.sh
操作 virtualenvwrapper 环境:
## 创建虚拟环境(本文以 mlia 为例) $ mkvirtualenv mlia # 创建完之后,自动切换到该环境下工作,可看到提示符变为: (mlia)$ # 在这个环境下安装的依赖不会影响到其他的环境 # 可通过如下两种方式查看该环境中安装的包 (mlia)$ pip list (mlia)$ lssitepackages # 退出虚拟环境 (mlia)$ deactivate # 从普通 Shell 环境切换到虚拟环境 $ workon mlia # 删除虚拟环境 $ rmvirtualenv mlia
在虚拟环境中,可以采用 pip 工具管理 Python 第三方模块,该环境中安装的第三方模块,不影响其他虚拟环境。使用 pip 工具时,不需要使用管理员 root 权限,使用普通账号权限即可。
Python Packaging User Guide(https://packaging.python.org)
Python 包管理工具解惑(http://www.tuicool.com/articles/FNJZNr)
virtualenvwrapper(http://blog.sina.com.cn/s/blog_55646c98010179og.html)