Pipenv is the officially recommended way of managing project dependencies.
安装pipenv
brew install pipenv # mac下homebrew安装brew upgrade pipenv # mac下homebrew升级
环境管理指令
# Create a new project using Python 3.6pipenv --python 3.6 # 只会生成Pipfilepipenv --python 3.6 install # 生成Pipfile和Pipfile.lock# pipenv会自动扫描系统寻找合适的版本信息,如果找不到的话,同时又安装了pyenv, 它会自动调用pyenv下载对应的版本的python# 激活当前项目的虚拟环境pipenv shell# 退出虚拟环境exit 或者 ctrl+d# locate the projectpipenv --where# locate the venvpipenv --venv# locate the Python interpreterpipenv --py
包管理指令
# 安装包pipenv install flaskpipenv install django=1.11.18# 升级包pipenv update requests# 卸载包pipenv uninstall flaskpipenv uninstall --all # 删除所有包# 安装dev包pipenv install --dev yapf # yapf是一个dev包pipenv install --dev # 根据当前项目的Pipfile安装dev包# 根据requirement.txt安装包pipenv install -r requirements.txt# 生成dev-packages的requirements.txt文件pipenv lock -r -d# 查看已经安装的依赖pipenv graph# 生成Pipfile.lockpipenv lock# 检查安装包是否存在安全隐患pipenv check# Use a lower-level pip command:pipenv run pip freeze
指定安装包的源
如果我们需要在安装包时,从一个源下载一个安装包,然后从另一个源下载另一个安装包,我们可以通过下面的方式配置。
[[source]]url = "https://pypi.python.org/simple"verify_ssl = truename = "pypi"[[source]]url = "http://pypi.home.kennethreitz.org/simple"verify_ssl = falsename = "home"[dev-packages][packages]requests = {version="*", index="home"}maya = {version="*", index="pypi"}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.
