Python环境管理(虚拟环境)/包管理工具

 

1. pipx 

github: https://github.com/pipxproject/pipx

documentation: https://pipxproject.github.io/pipx/installation/

1.1 简介

pipx是一个可以安装运行Python Application在一个独立环境(指的是单独的Python virtual environment)的工具。

1.2 安装/运行

安装过程比较简单,比如使用pip 安装:

//python 3.6+ is required to install pipx.
python3 -m pip install --user pipx python3 -m pipx ensurepath //ensurepath 的作用参考https://pipxproject.github.io/pipx/docs/

//examples
pipx run xxx
//更多参考这里 https://pipxproject.github.io/pipx/examples/

其他有关安装的详细内容可以参考Documentation

1.3 和其他工具的对比

可以参考一下这里

个人观点:pipx虽然也可以创建虚拟环境和安装Python package。但是更适合于安装运行一个Application,而不是做开发测试。

 

2. pipenv

github: https://github.com/pypa/pipenv

documentation: https://pipenv.pypa.io/en/latest/#install-pipenv-today

2.1 简介

来看一段github上的介绍:

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.

写的很清楚,基本上pyenv就是用来创建/管理虚拟环境以及添加/删除Python packages, 特别适合Python project的开发测试(不再需要单独使用Python virtual environment管理工具比如venv以及单独使用pip安装各种包)。另外一个优点也是Ken Reitz这里一篇文章描述的python package management的问题

2.2 安装运行

推荐使用pipx来安装pipenv:

$ pipx install pipenv

其他的安装方式以及细节参考这里

详细的用法如下(更多参考github以及documentation)

$ 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.
  --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:
   $ 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.

 

3. tox

github: https://github.com/tox-dev/tox

documentation: https://tox.readthedocs.io/en/latest/

3.1 简介

先看下官方文档上的说明:

tox aims to automate and standardize testing in Python. It is part of a larger vision of easing the packaging, testing and release process of Python software.

tox is a generic virtualenv management and test command line tool you can use for:

  • checking that your package installs correctly with different Python versions and interpreters

  • running your tests in each of the environments, configuring your test tool of choice

  • acting as a frontend to Continuous Integration servers, greatly reducing boilerplate and merging CI and shell-based testing.

 以上描述说的很明确了, tox主要是用来自动化测试Python代码的一个工具,它集成了Python虚拟环境的管理、依赖包管理以及自动化运行的脚本,是一个非常棒的自动化测试工具(事实上也是作者目前工作在用的工具)。

 

3.2 安装运行

安装比较简单,可以使用pip直接安装,也可以通过clone源码来安装,貌似也可以通过有些linux或者Mac系统的软件源来安装(本人没试过)。细节部分可以参考这里

pip install tox

另外,tox本身是可以安装在Python的虚拟环境中的,可以试下用pipx来安装tox。

运行的话,直接tox就可以:

tox

usage: tox [--version] [-h] [--help-ini] [-v] [-q] [--showconfig] [-l] [-a] [-c CONFIGFILE] [-e envlist] [--devenv ENVDIR] [--notest] [--sdistonly] [--skip-pkg-install] [-p [VAL]] [-o]
           [--parallel--safe-build] [--installpkg PATH] [--develop] [-i URL] [--pre] [-r] [--result-json PATH] [--discover PATH [PATH ...]] [--hashseed SEED] [--force-dep REQ]
           [--sitepackages] [--alwayscopy] [-s [val]] [--workdir PATH]
           [args [args ...]]

// 常用的命令
tox -c 'path/to/dir_with_tox_ini'

//更多参考
https://tox.readthedocs.io/en/latest/config.html的cli部分

 

3.3 配置

配置文件对于tox是一个比较重要的部分。先看下官方的simple example:

// config file tox.ini

# content of: tox.ini , put in same dir as setup.py
[tox]
envlist = py27,py36

[testenv]
# install pytest in the virtualenv where commands will be executed
deps = pytest
commands =
    # NOTE: you can run any command line tool here - not just tests
    pytest

上面这个配置文件指定了要在py27和py36上的环境上来执行pytest运行目录下相关的tests.

更多参考这里

后面会专门针对tox的配置再做几个详细的篇章。

 

posted @ 2020-09-06 22:33  LCdre  阅读(983)  评论(0编辑  收藏  举报