我们可以通过out of fashion的setup.py安装python模块,比如:

1
2
3
4
5
6
7
8
9
10
11
setup.py:

% python setup.py build #编译

% python setup.py install #安装

% python setup.py sdist #制作分发包

% python setup.py bdist_wininst #制作windows下的分发包

% python setup.py bdist_rpm

除了setup.pyeasy_install安装包外,pip是最常用也是最方便的包管理器。

Pip

pip 是一个 关于Python 的包管理程序,可以用于安装和管理 Python 包,目前python>=3.6已默认安装好pip,升级的话,可用升级命令python -m pip install --upgrade pip, 目前最新版本为20.1

1
python -m pip install --upgrade pip

安装、查看、更新包

1
2
3
4
pip install SomePackage ## 安装包名
pip install SomePackage==1.0.0 ## 明确安装包指定版本号:
pip install SomePackge>=1.0.0 ## 指定安装包最小版本号:
pip install SomePackage>=1.0.0,<2.0.0 ## 指定安装包版本号区间

查看包信息:

1
pip show --files django > django_pkg.txt
1
2
3
4
5
6
7
8
9
10
11
12
Name: Django  ## 包名
Version: 3.0.6 ## 版本号
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: d:\python\lib\site-packages ## 安装位置
Requires: pytz, sqlparse, asgiref ## 依赖包
Required-by: djangorestframework, django-taggit, django-simpleui, django-simple-captcha, django-rosetta, django-ranged-response, django-model-utils, django-markdownx, django-hreflang, django-filter, django-debug-toolbar, django-cors-headers, django-appconf, django-allauth
Files:
--snip--

检查哪些包需要更新

1
pip list --outdated

升级包

1
2
pip install --upgrade 升级的包名
# pip install -U 升级的包名

如果遇到 ERROR: Could not install packages due to an EnvironmentError: [WinError 145] 目录不是空的

此错误可直接忽略,实际已安装

如果遇到WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

未添加anaconda的环境变量,需添加:

1
2
3
D:\Apps\conda
D:\Apps\conda\Scripts
D:\Apps\conda\Library\bin

pip卸载包

1
pip uninstall 卸载的包名

若添加user参数,比如:pip install package_name --user,则安装的第三方库时只对当前用户可见,安装目录为:

1
2
C:\Users\your_user_name\AppData\Roaming\Python\Python37\site-packages(windows)
/home/your_user_name/.local/lib/python3.7/site-packages/**(ubuntu)

升级所有可升级的包

1
2
pip install pip-review
pip-review --local --interactive

注:通过Subprocess更新所有包在高版本pip下已不可用。

Linux上还可用以下命令升级所有包

1
pip freeze --local | grep -v '^-e' | cut -d = -f 1  | xargs -n1 pip install -U
1
pip list -o --format legacy|awk '{print $1}'` ; do pip install --upgrade $i; done

pip 也支持直接从文件如requirements.txt读取包列表以便批量安装,命令为 pip install -r requirements.txt

requirements.txt示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## Crawler
requests
# lxml html5lib
beautifulsoup4
pyquery
selenium
# tesserocr
tesseract
tornado
pyspider
scrapy
scrapy-redis
### twisted use conda instead

## for databases
redis
psycopg2
pymysql

## Django
Django>=2.2.5
django-allauth>=0.42.0
django-crispy-forms>=1.9.1
django-debug-toolbar>=2.2
django-simpleui>=4.0.2
djangorestframework>=3.11.0
django-taggit>=1.3.0
django-hreflang>=2.2
markdown>=3.2.1
django-filter>=2.2.0
django-taggit-serializer>=0.1.7
awesome-slugify>=1.6.5
pillow>=7.1.2
django-imagekit>=4.0.2
django-rosetta>=0.9.4
django-hreflang>=2.2
django-markdownx>=3.0.1
django-simple-captcha>=0.5.12

## SCI
matplotlib>=3.2.2
scipy>=1.5.1
pandas>=1.0.5
scikit-learn
nltk
jieba
keras

## for life
py2exe
you-get
pyexcel

## for style
pycodestyle
PyYAML
## for Cipher
pycryptodomex

## for bio
pyfaidx

修改默认源

pip默认源速度很慢,这时可使用第三方源来提高速度,比如:

阿里云 http://mirrors.aliyun.com/pypi/simple/
  中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  豆瓣(douban) http://pypi.douban.com/simple/
  清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

等等。

如果你只是临时使用,那可以在使用pip命令的在后面加上-i参数,指定pip源,如:pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

对于windows用户

可直接在用户目录比如Administrator(%HOMEPATH%)目录中创建一个pip目录,在pip 目录下新建文件pip.ini,内容如下:

1
2
3
4
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

对于Linux用户
可修改文件vim ~/.pip/pip.conf (没有就创建一个), 内容如下:

1
2
3
4
5
[global]

index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com

如果安装过程中出现版本错误,比如:

1
2
3
4
5
6
7
Complete output (4 lines):
This backport is meant only for Python 2.
It does not work on Python 3, and Python 3 users do not need it as the concu
rrent.futures package is available in the standard library.
For projects that work on both Python 2 and 3, the dependency needs to be co
nditional on the Python version, like so:
extras_require={':python_version == "2.7"': ['futures']}

请换用官方源安装。

帮助命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
pip --help

Usage:
pip<command>[options]

Commands:
install 安装包.
uninstall 卸载包.
freeze 按着一定格式输出已安装包列表
list 列出已安装包.
show 显示包详细信息.
search 搜索包,类似yum里的search.
wheel Buildwheelsfromyourrequirements.
zip 不推荐.Zipindividualpackages.
unzip 不推荐.Unzipindividualpackages.
bundle 不推荐.Createpybundles.
help 当前帮助.

GeneralOptions:
-h,--help 显示帮助.
-v,--verbose 更多的输出,最多可以使用3次
-V,--version 现实版本信息然后退出.
-q,--quiet 最少的输出.
--log-file<path> 覆盖的方式记录verbose错误日志,默认文件:/root/.pip/pip.log
--log<path> 不覆盖记录verbose输出的日志.
--proxy<proxy> Specifyaproxyintheform[user:passwd@]proxy.server:port.
--timeout<sec> 连接超时时间(默认15秒).
--exists-action<action> Defaultactionwhenapathalreadyexists:(s)witch,(i)gnore,(w)ipe,(b)ackup.
--cert<path> 证书.

ENV

虚拟环境:一个独立的python开发环境,防止版本冲突,也便于包的管理,类似于Javacript界远近闻名的node.js一样,除了多占用点磁盘空间(node_modules)。

Venv

1
2
3
4
python -m venv . 
python -m venv my_django
## Win activate.bat
source activate

Virtualenv

Virtualenv用于建立一个独立的虚拟python环境, 它可以针对不同的项目建立独立的依赖而不相互冲突,并且安装Python模块的时候也不需要任何管理员权限。

1
2
3
$ which python3
$sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
$sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 200
1
sudo apt install python3.7
1
2
3
$sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1

$sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
1
2
3
4
5
6
7
8
9
10
11
12
$ python -V
Python 3.7.5
$ pip install virtualenv
# 创建一个独立python环境my_django
# 指定python版本
# virtualenv my_django -p $(which python3)
$ virtualenv my_django # mkdir env virtualenv env/my_django
# my_django激活虚拟环境
# 激活后,会在console显示该虚拟环境名称
$ source my_django/bin/activate
# 输入deactivate来退出虚拟环境
$ deactivate

Pipenv

基本理念

  • Pipfile 文件是 TOML 格式
  • 一个项目对应一个 Pipfile,支持开发环境与正式环境区分。默认提供 defaultdevelopment 区分。
  • 提供版本锁支持,存为 Pipfile.lock

pipefile示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
django-crispy-forms = "==1.7.2"
pytz = "==2019.2"
sqlparse = "==0.3.0"
Django = "==2.2"
django-registration = "*"
gunicorn = "*"
whitenoise = "==3.3.1"

[requires]
python_version = "3.7"

pipenv

pipenvPipfile 主要倡导者、requests 作者 Kenneth Reitz 的一个库,有机的结合了 Pipfile 、pip 和 virtualenv。

主要特性

  • 根据 Pipfile 自动寻找项目根目录。
  • 如果不存在,可以自动生成 PipfilePipfile.lock
  • 自动在项目目录的 .venv 目录创建虚拟环境。(暂时需要设置 export PIPENV_VENV_IN_PROJECT=1
  • 自动管理 Pipfile 新安装和删除的包。
  • 自动更新 pip。

基本命令

  • pipenv --where:寻找项目根目录。
  • pipenv install:安装 Pipfile 中所列的所有包。
  • pipenv install --dev:安装 Pipfile 中 dev 环境所列的所有包。
  • pipenv uninstall:卸载所有包。
  • pipenv install pytest --dev:在 dev 环境中安装 pytest 包。
  • pipenv lock:确认 Pipfile 中所有包已安装,并根据安装版本生成 Pipfile.lock
  • pipenv shell:应用虚拟环境。

REFERENCES