Yifei Kong

May 30, 2017

Python pip 极简教程

让 pip 使用国内的源

  • 阿里云 https://mirrors.aliyun.com/pypi/simple/
  • 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  • 豆瓣 http://pypi.douban.com/simple/  清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/  中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple/

修改 ~/.pip/pip.conf (没有就创建一个), 内容如下:

[global]
https://pypi.tuna.tsinghua.edu.cn/simple/

之前的 pip 教程

installing packages

pip install <package-name>==<version>, version is optional

To install a package from git repository:

pip install -e git+REPO

pip currently supports cloning over git, git+https and git+ssh:

Here are the supported forms:

[-e] git+git://git.myproject.org/MyProject#egg=MyProject
[-e] git+https://git.myproject.org/MyProject#egg=MyProject
[-e] git+ssh://git.myproject.org/MyProject#egg=MyProject  # for private repo, you can only use this
[-e] git+git@git.myproject.org:MyProject#egg=MyProject

Passing branch names, a commit hash or a tag name is possible like so:
[-e] git://git.myproject.org/MyProject.git@master#egg=MyProject
[-e] git://git.myproject.org/MyProject.git@v1.0#egg=MyProject
[-e] git://git.myproject.org/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709#egg=MyProject

if you add the -e(editable) option, then you can save the version info in freeze, which is exactly what you need.

upgrade a package:

pip install package-name --upgrade
pip install xxx -U

忽略缓存

pip install --on-cache-dir xxx

requirements file

save current dependencies to a requirement file:

pip freeze > requirements.txt

install from a requirement file

pip install -r requirements.txt

http://crazygit.wiseturtles.com/2018/01/08/pipenv-tour/

卸载的时候删除所有依赖

可以使用 pip-autoremove 包

pip install pip-autoremove
pip-autoremove requrests -y  # requests 有严重的内存泄露,而且代码非常不优雅,强烈建议不要使用。

新工具 pipenv

pipenv 太垃圾了,吹得很牛逼,结果基本不能用。不知道为啥不使用官方的 venv,而要使用 virtualenv,简直脑残。后面的教程请忽略。

-> % cd repos/crawler
yifei@Yifeis-MacBook [07:14:07] [~/repos/crawler]
-> % pipenv --python 3
Creating a virtualenv for this project…
Using /usr/local/bin/python3 (3.6.5) to create virtualenv…
⠋Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/yifei/repos/.venv/bin/python3.6
Also creating executable in /Users/yifei/repos/.venv/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /Users/yifei/repos/.venv

相关的 issue:https://github.com/pypa/pipenv/issues/70

原文:

pip 是一个简单的 python 的包管理工具,相比于 node 的npm 或者 rust 的cargo 来说,功能非常弱,不过python官方已经出了一个新的工具——pipenv,终于跟上了时代。

pipenv 结合了之前 pip 和 venv/virtualenv 的功能,不再使用 requirements.txt 来管理。

安装

直接全局安装,不要在 venv 中安装。建议使用 python3 的 pip 安装。

pip(3) install pipenv

使用

创建虚拟环境

如果当前目录没有虚拟环境,pipenv会在运行 pipenv install 的时候自动创建一个,不过也可以手工创建。首先设定环境变量 PIPENV_VENV_IN_PROJECT,这样pipenv 就会在当前目录下的.venv 目录创建虚拟环境,而不是在系统的某个目录中统一创建。可以参考这里。使用 --python 参数可以指定创建的虚拟环境的版本,比如 2、3、3.5 等等。当然得是你系统中已经安装的版本。

-> % pwd
~/repos/test/

-> % export $PIPENV_VENV_IN_PROJECT=1

-> % pipenv --python 3

Creating a virtualenv for this project…
Using /usr/local/bin/python3 (3.6.5) to create virtualenv…
⠋Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/yifei/repos/test/.venv/bin/python3.6
Also creating executable in /Users/yifei/repos/test/.venv/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /Users/yifei/repos/test/.venv

安装库

安装库可以使用 == 指定版本信息,当然也可以不指定。

pipenv install requests==2.13.0

或者直接使用

pipenv install

pipenv 会直接从 requirements.txt 或者 Pipfile 中安装包。或者使用-r手工指定之前的 requirements.txt 的位置。

需要注意的是,pivenv install 会直接修改 Pipfile,而不需要--save参数。

锁定

使用 pipenv lock 可以生成当前安装的版本的锁定文件 Pipfile.lock。

运行

当运行脚本的时候直接使用 pipenv run python main.py 就可以避免手工启用 venv 了。

如果配置了 .env 文件,pipenv run 的时候也会加载里面的环境变量。