Pipenv is the officially recommended way of managing project dependencies.

安装pipenv

  1. brew install pipenv # mac下homebrew安装
  2. brew upgrade pipenv # mac下homebrew升级

环境管理指令

  1. # Create a new project using Python 3.6
  2. pipenv --python 3.6 # 只会生成Pipfile
  3. pipenv --python 3.6 install # 生成Pipfile和Pipfile.lock
  4. # pipenv会自动扫描系统寻找合适的版本信息,如果找不到的话,同时又安装了pyenv, 它会自动调用pyenv下载对应的版本的python
  5. # 激活当前项目的虚拟环境
  6. pipenv shell
  7. # 退出虚拟环境
  8. exit 或者 ctrl+d
  9. # locate the project
  10. pipenv --where
  11. # locate the venv
  12. pipenv --venv
  13. # locate the Python interpreter
  14. pipenv --py

包管理指令

  1. # 安装包
  2. pipenv install flask
  3. pipenv install django=1.11.18
  4. # 升级包
  5. pipenv update requests
  6. # 卸载包
  7. pipenv uninstall flask
  8. pipenv uninstall --all # 删除所有包
  9. # 安装dev包
  10. pipenv install --dev yapf # yapf是一个dev包
  11. pipenv install --dev # 根据当前项目的Pipfile安装dev包
  12. # 根据requirement.txt安装包
  13. pipenv install -r requirements.txt
  14. # 生成dev-packages的requirements.txt文件
  15. pipenv lock -r -d
  16. # 查看已经安装的依赖
  17. pipenv graph
  18. # 生成Pipfile.lock
  19. pipenv lock
  20. # 检查安装包是否存在安全隐患
  21. pipenv check
  22. # Use a lower-level pip command:
  23. pipenv run pip freeze

指定安装包的源

如果我们需要在安装包时,从一个源下载一个安装包,然后从另一个源下载另一个安装包,我们可以通过下面的方式配置。

  1. [[source]]
  2. url = "https://pypi.python.org/simple"
  3. verify_ssl = true
  4. name = "pypi"
  5. [[source]]
  6. url = "http://pypi.home.kennethreitz.org/simple"
  7. verify_ssl = false
  8. name = "home"
  9. [dev-packages]
  10. [packages]
  11. requests = {version="*", index="home"}
  12. maya = {version="*", index="pypi"}
  13. records = "*"

如上设置了两个源:

同时指定requests包从home源下载,maya包从pypi源下载。

Pipfile.lock

Pipfile.lock is super important because it does two things:

  • Provides good security by keeping a hash of each package installed.
  • Pins the versions of all dependencies and sub-dependencies, giving you replicable environments.