Python pip & env
我们可以通过out of fashion的setup.py安装python模块,比如:
1 | setup.py: |
除了setup.py和easy_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 | pip install SomePackage ## 安装包名 |
查看包信息:
1 | pip show --files django > django_pkg.txt |
1 | Name: Django ## 包名 |
检查哪些包需要更新
1 | pip list --outdated |
升级包
1 | pip install --upgrade 升级的包名 |
如果遇到 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 | C:\Users\your_user_name\AppData\Roaming\Python\Python37\site-packages(windows) |
升级所有可升级的包
1 | pip install pip-review |
注:通过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 | ## Crawler |
修改默认源
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 | [global] |
对于Linux用户
可修改文件vim ~/.pip/pip.conf (没有就创建一个), 内容如下:
1 | [global] |
如果安装过程中出现版本错误,比如:
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 | pip --help |
ENV
虚拟环境:一个独立的python开发环境,防止版本冲突,也便于包的管理,类似于Javacript界远近闻名的node.js一样,除了多占用点磁盘空间(node_modules)。
Venv
1 | python -m venv . |
Virtualenv
Virtualenv用于建立一个独立的虚拟python环境, 它可以针对不同的项目建立独立的依赖而不相互冲突,并且安装Python模块的时候也不需要任何管理员权限。
1 | $ which python3 |
1 | sudo apt install python3.7 |
1 | $sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 |
1 | $ python -V |
Pipenv
基本理念
Pipfile文件是 TOML 格式- 一个项目对应一个
Pipfile,支持开发环境与正式环境区分。默认提供default和development区分。 - 提供版本锁支持,存为
Pipfile.lock。
pipefile示例:
1 | [[source]] |
pipenv
pipenv 是 Pipfile 主要倡导者、requests 作者 Kenneth Reitz 的一个库,有机的结合了 Pipfile 、pip 和 virtualenv。
主要特性
- 根据
Pipfile自动寻找项目根目录。 - 如果不存在,可以自动生成
Pipfile和Pipfile.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
-
virtualenv官方Doc: https://virtualenv.pypa.io/en/latest/
-
virtualenvwrapper可方便创建和管理系统中的所有虚拟环境: -
virtualenv: https://www.liaoxuefeng.com/wiki/1016959663602400/1019273143120480



