python 工具链 虚拟环境和包管理工具 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.python

pipenv 是Kenneth Reitz大神的做品,可以有效管理Python多个环境,各类包。过去咱们通常用virtualenv搭建虚拟环境,管理python版本,可是跨平台的使用不太一致,且有时候处理包之间的依赖总存在问题;过去也经常用 pip进行包的管理,pip已经足够好,可是仍然推荐pipenv,至关于virtualenv和pip的合体,且更增强大。shell

pipenv主要有如下特性

  • pipenv集成了pip,virtualenv二者的功能,且完善了二者的一些缺陷。
  • 过去用virtualenv管理requirements.txt文件可能会有问题,Pipenv使用Pipfile和Pipfile.lock,后者存放将包的依赖关系,查看依赖关系是十分方便。
  • 各个地方使用了哈希校验,不管安装仍是卸载包都十分安全,且会自动公开安全漏洞。。
  • 经过加载.env文件简化开发工做流程。
  • 支持Python2 和 Python3,在各个平台的命令都是同样的。

安装

pip install pipenv

Usages

$ pipenv
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  # 输出项目目录
  --where          Output project home information.
  # 显示虚拟环境目录
  --venv           Output virtualenv information.
  # python可执行命令路径
  --py             Output Python interpreter information.
  # pipenv环境变量
  --envs           Output Environment Variable options.
  # 删除
  --rm             Remove the virtualenv.
  --bare           Minimal output.
  --completion     Output completion (to be eval'd).
  --man            Display manpage.
  # 建立虚拟环境
  --three / --two  Use Python 3/2 when creating virtualenv.
  --python TEXT    Specify which version of Python virtualenv should use.
  
  --site-packages  Enable site-packages for the virtualenv.
  --version        Show the version and exit.
  -h, --help       Show this message and exit.


Usage Examples:
   Create a new project using Python 3.7, specifically:
   # 用 python3.7 建立一个project
   $ pipenv --python 3.7

	 # 删除 project
   Remove project virtualenv (inferred from current directory):
   $ pipenv --rm

   # 根据 Pipfile 初始化环境(包括 dev 环境)
   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

	 # 使用本地setup.py在虚拟环境/Pipfile安装一个软件
   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   # 使用低级别的pip命令
   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.

Locate the project:npm

$ pipenv --where
/Users/kennethreitz/Library/Mobile Documents/com~apple~CloudDocs/repos/kr/pipenv/test

Locate the virtualenv:json

$ pipenv --venv
/Users/kennethreitz/.local/share/virtualenvs/test-Skyy4vre

Locate the Python interpreter:windows

$ pipenv --py
/Users/kennethreitz/.local/share/virtualenvs/test-Skyy4vre/bin/python

Install packages:安全

$ pipenv install
Creating a virtualenv for this project...
...
No package provided, installing all dependencies.
Virtualenv location: /Users/kennethreitz/.local/share/virtualenvs/test-EJkjoYts
Installing dependencies from Pipfile.lock...
...

To activate this project's virtualenv, run the following:
$ pipenv shell

加速

在Pipfile中增长bash

[[source]]
name = "pypi"
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
verify_ssl = tru

开发流程

若是在开发环境已经完成开发,如何构建生产环境呢?这时候就要使用Pipfile.lock了,运行如下命令,把当前环境的模块lock住, 它会更新Pipfile.lock文件,该文件是用于生产环境的,你永远不该该编辑它。app

$ pipenv lock

而后只须要把代码和Pipfile和Pipfile.lock放到生产环境,运行下面的代码,就能够建立和开发环境同样的环境,Pipfile.lock里记录了全部包和子依赖包的确切版本,所以是肯定构建composer

$ pipenv install

若是要在另外一个开发环境作开发,则将代码和Pipfile复制过去,运行如下命令:ide

安装包括 dev 开发中的包

$ pipenv install --dev

执行命令

cat Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
requests = "*"
jinja2 = "*"
tobe = "*"

[requires]
python_version = "2.7"

[scripts]
start = 'python main.py'
list = 'pip list'

pipenv run start

vscode使用pipenv虚拟环境

  1. 获取当前虚拟环境的位置

    pipenv --venv
  2. 编辑 user 的setting.json

    {
        // Necessary for pipenv
        "python.pythonPath": "/Users/yangyanan/.local/share/virtualenvs/daily_report-lBui7cTH/bin/python",
        // For unsolved-import
        "python.jediEnabled": true,
        // Limit for Editor windows
        "workbench.editor.limit.value": 5
    }

参考

  1. 视频教程 https://www.youtube.com/watch?v=Ib1vO2Tbogo&t=12s
相关文章
相关标签/搜索