Skip to content

Latest commit

 

History

History
104 lines (78 loc) · 2.92 KB

pipenv_intro.md

File metadata and controls

104 lines (78 loc) · 2.92 KB

pipenv 使用简介

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.

It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.

Pipenv is primarily meant to provide users and developers of applications with an easy method to setup a working environment. For the distinction between libraries and applications and the usage of setup.py vs Pipfile to define dependencies, see ☤ Pipfile vs setup.py.

总结成一句话: pipenv类似于node的npm, rust的cargo, 把原来开发流程中virtualenv和pip组合用法整合到一个工具中来了。


安装

MacOS

brew install pipenv

Other Platform

pip install pipenv

基本用法

创建虚拟环境

如果是老项目本身已经存在虚拟环境, pipenv会自动检测到。

创建新的virtualenv虚拟环境

pipenv --two  使用Python2创建virtualenv
pipenv --three 使用Python3创建virtualenv
pipenv --python 3.6.5 使用指定的Python版本创建virtualenv

创建成功后, 会自动进入virtualenv。

退出virtualenv虚拟环境

exit

重新进入virtualenv虚拟环境

pipenv shell

查看当前virtualenv虚拟环境路径信息

pipenv --venv

项目依赖包安装

安装一个新包

pipenv install request django ...

如果项目本身有requirements.txt, 可以指定从requirements.txt 安装包:

pipenv install -r path/to/requirements.txt

可以在安装virtualenv同时安装相关依赖包:

pipenv install -r path/to/requirements.txt --python 3.6.5

安装完成后在项目当前目录会生成 PipfilePipfile.lock 两个文件。 默认pipenv是从官方pypi源去安装依赖包, 国内用户访问会比较慢, 我们可以编辑 Pipfile 中相关内容,使用国内的阿里云pypi镜像源。

[[source]]
url = "http://mirrors.aliyun.com/pypi/simple"
verify_ssl = false
name = "pypi"

Pipfile中的包分成了productiondev两种环境的包, 本地开发可以直接安装dev环境相关的包:

pipenv install -d[--dev]

生产环境 安装包建议使用Pipfile.lock来安装:

pipenv install --ignore-pipfile

注意事项(best practice)

部署系统依赖

可以使用--system参数告诉pipenv将Pipfile中的包安装到父系统中, 而不是安装到virtualenv里,特别是在容器部署的时候:

pipenv install --system --deploy

更多用法参考: Pipenv