$ ls ~yifei/notes/

Python 中的包管理工具

Posted on:

Last modified:


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


可以使用 pip-autoremove 包

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


众所周知,Python 的依赖管理是非常拉跨的,甚至都不支持把开发依赖和运行时的依赖分开。这就导致了调试时用到的 ipdb 等等还会出现在 requirements.txt 的问题。我做了一个简单的 work-around, 先把开发依赖装好了保存到 dev-requirements.txt 中,然后再装运行时依赖,每次 freeze 的时候把开发依赖剔除掉就好了。毕竟用到的开发时依赖还是比较少的,也不怎么变,这种方法基本能解决问题。

然后神器来了:pip install pipdeptree, 他可以把依赖显示成树形,这样我们只要根部的依赖就好啦。


pip install ipython ipdb black isort pipdeptree

然后保存到 dev-requirements.txt 中:

pipdeptree -f | grep '^\w' > dev-requirements.txt

然后再安装依赖,比如 pyyaml redis, 然后保存的时候过滤掉开发依赖:

pipdeptree -f | grep '^\w' | grep -vxFf dev-requirements.txt > requirements.txt

还可以写一个 Makefile:

    pipdeptree -f | grep '^\w' | grep -vxFf dev-requirements.txt > requirements.txt

这样只要 make freeze 就好啦

让 pip 使用国内的源

pip install flask -i https://pypi.tuna.tsinghua.edu.cn/simple/

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



pipenv 太垃圾了,吹得很牛逼,结果基本不能用。不知道为啥不使用官方的 venv,而要使用 virtualenv。强烈不建议使用。


  1. http://crazygit.wiseturtles.com/2018/01/08/pipenv-tour/
  2. https://stackoverflow.com/questions/5900201/pip-freeze-without-dependencies-of-installed-packages

© 2016-2022 Yifei Kong. Powered by ynotes

All contents are under the CC-BY-NC-SA license, if not otherwise specified.

Opinions expressed here are solely my own and do not express the views or opinions of my employer.