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-importantPipfile.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的配置再做幾個詳細的篇章。