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.

pipenv 是Kenneth Reitz大神的作品,能够有效管理Python多个环境,各种包。过去我们一般用virtualenv搭建虚拟环境,管理python版本,但是跨平台的使用不太一致,且有时候处理包之间的依赖总存在问题;过去也常常用 pip进行包的管理,pip已经足够好,但是仍然推荐pipenv,相当于virtualenv和pip的合体,且更加强大。

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:

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

Locate the virtualenv:

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

Locate the Python interpreter:

$ 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中增加

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

开发流程

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

$ pipenv lock

然后只需要把代码和Pipfile和Pipfile.lock放到生产环境,运行下面的代码,就可以创建和开发环境一样的环境,Pipfile.lock里记录了所有包和子依赖包的确切版本,因此是确定构建

$ pipenv install

如果要在另一个开发环境做开发,则将代码和Pipfile复制过去,运行以下命令:

安装包括 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

猜你喜欢

转载自www.cnblogs.com/hiyang/p/12631548.html