pipenv 的使用

pipenv 的使用

pipenv 简介

pipenv Python官方推荐 的包管理工具。能够说,它集成了virtualenv, pippyenv三者的功能。其目的旨在集合了全部的包管理工具的长处,如: npm, yarn, composer等的优势。python

它可以自动为项目建立和管理虚拟环境,从Pipfile文件添加或删除安装的包,同时生成Pipfile.lock来锁定安装包的版本和依赖信息,避免构建错误。git

pipenv主要解决了以下问题:github

  • 不用再单独使用pipvirtualenv, 如今它们合并在一块儿了
  • 不用再维护requirements.txt, 使用PipfilePipfile.lock来代替
  • 能够使用多个python版本(python2python3)
  • 在安装了pyenv的条件下,能够自动安装须要的Python版本

pipenv 安装

pip install pipenv -i https://pypi.douban.com/simple

pipenv 命令

Usage: pipenv \[OPTIONS\] COMMAND \[ARGS\]...  
​  
Options:  
 --where             Output project home information.  
 --venv              Output virtualenv information.  
 --py                Output Python interpreter information.  
 --envs              Output Environment Variable options.  
 --rm                Remove the virtualenv.  
 --bare              Minimal output.  
 --completion        Output completion (to be eval'd).  
 --man               Display manpage.  
 --support           Output diagnostic information for use in GitHub issues.  
 --site-packages     Enable site-packages for the virtualenv.  \[env var:  
 PIPENV\_SITE\_PACKAGES\]  
 --python TEXT       Specify which version of Python virtualenv should use.  
 --three / --two     Use Python 3/2 when creating virtualenv.  
 --clear             Clears caches (pipenv, pip, and pip-tools).  \[env var:  
 PIPENV\_CLEAR\]  
 -v, --verbose       Verbose mode.  
 --pypi-mirror TEXT  Specify a PyPI mirror.  
 --version           Show the version and exit.  
 -h, --help          Show this message and exit.  
​  
​  
Usage Examples:  
 Create a new project using Python 3.7, specifically:  
 $ pipenv --python 3.7  
​  
 Remove project virtualenv (inferred from current directory):  
 $ pipenv --rm  
​  
 Install all dependencies for a project (including dev):  
 $ pipenv install --dev  
​  
 Create a lockfile containing pre-releases:  
 $ pipenv lock --pre  
​  
 Show a graph of your installed dependencies:  
 $ pipenv graph  
​  
 Check your installed dependencies for security vulnerabilities:  
 $ pipenv check  
​  
 Install a local setup.py into your virtual environment/Pipfile:  
 $ pipenv install -e .  
​  
 Use a lower-level pip command:  
 $ pipenv run pip freeze  
​  
Commands:  
 check      Checks for security vulnerabilities and against PEP 508 markers  
 provided in Pipfile.  
 clean      Uninstalls all packages not specified in Pipfile.lock.  
 graph      Displays currently-installed dependency graph information.  
 install    Installs provided packages and adds them to Pipfile, or (if no  
 packages are given), installs all packages from Pipfile.  
 lock       Generates Pipfile.lock.  
 open       View a given module in your editor.  
 run        Spawns a command installed into the virtualenv.  
 shell      Spawns a shell within the virtualenv.  
 sync       Installs all packages specified in Pipfile.lock.  
 uninstall  Un-installs a provided package and removes it from Pipfile.  
 update     Runs lock, then sync.

pipenv 经常使用命令介绍

# 安装包
$ pipenv install

# 安装指定包版本
eg: $ pipenv install django=2.1.7

# 安装 pipfile.lock 中固定版本
$ pipenv install --ingnore-pipfile

# 激活(进入)当前项目的虚拟环境
$ pipenv shell

# 查看当前虚拟环境的路径
$ pipenv --venv

# 安装开发依赖包
$ pipenv install pytest --dev

# 图形显示包依赖关系(可检查安装包对其余包版本的要求)
$ pipenv graph

# 生成lockfile(pipenv install 默认会生成 lockfile 文件)
$ pipenv lock

# 删除某个安装包
$ pipenv uninstall django

# 删除全部的安装包
$ pipenv uninstall --all

# 退出当前虚拟环境
$ exit or ctrl+d

# 删除当前虚拟环境
$ pipenv --rm

pipenv 高级

  • 导入:
当在执行 pipenv install命令的时候,若是有一个 requirements.txt文件,那么会自动从 requirements.txt文件导入安装包信息并建立一个 Pipfile文件。

一样能够使用$ pipenv install -r path/to/requirements.txt来导入requirements.txt文件shell

  • 指定 python 版本

$ pipenv --python 3
$ pipenv --python 3.6
$ pipenv --python 2.7.14npm

咱们也能够从PipfilePipfile.lock文件来生成requirements.txtdjango

  • 生成 requirements.txt 文件

注意⚠️:pipenv会自动扫描系统寻找合适的版本信息,若是找不到的话,同时又安装了pyenv, 它会自动调用pyenv下载对应的版本的python安全

# 生成requirements.txt文件
$ pipenv lock -r

# 生成dev-packages的requirements.txt文件
# pipenv lock -r -d

image.png

  • 排查安全隐患

pipenv包含了safety模块,能够让咱们坚持安装包是否存在安全隐患。composer

pipenv check
  • 代码风格检查

pipenv默认集成了flake8, 能够用来检测编码风格ide

pipenv check --style test.py
相关文章
相关标签/搜索