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

1. pipx

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

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.--rmRemove the virtualenv.--bare Minimal output.--completion Output completion (to be eval'd).

--manDisplay manpage.--three / --two Use Python 3/2when creating virtualenv.--python TEXT Specify whichversion of Python virtualenv should use.--site-packages Enable site-packages forthe virtualenv.--version Show the version and exit.-h, --help Show this message and exit.

Usage Examples:

Create a new project using Python3.7, specifically:

$ pipenv--python 3.7Remove project virtualenv (inferred from current directory):

$ pipenv--rmInstall all dependenciesfora project (including dev):

$ pipenvinstall --dev

Create alockfile containing pre-releases:

$ pipenv lock--pre

Show a graph of your installed dependencies:

$ pipenv graph

Check your installed dependenciesforsecurity vulnerabilities:

$ pipenv check

Install a local setup.py into your virtual environment/Pipfile:

$ pipenvinstall -e .

Use a lower-level pip command:

$ pipenv run pip freeze

Commands:

check Checksfor security vulnerabilities and against PEP 508markers

providedinPipfile.

clean Uninstalls all packages not specifiedinPipfile.lock.

graph Displays currently–installed dependency graph information.install Installs provided packages and adds them to Pipfile, or (ifno

packages are given), installs all packages from Pipfile.

lock Generates Pipfile.lock.

open View a given moduleinyour editor.

run Spawns a command installed into the virtualenv.

shell Spawns a shell within the virtualenv.sync Installs all packages specified inPipfile.lock.

uninstall Un-installs a provided package and removes it from Pipfile.

3. tox

3.1 简介

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

toxaims 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 , putin same diras setup.py

[tox]

envlist=py27,py36

[testenv]

#install pytest inthe 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的配置再做几个详细的篇章。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值