Package: peeve
peeveMy pet peeve: Wrangling with virtual environments for simple scripts.peeve provides a virtual environment and runs your script in a single command:peevescript.pyor with the shorter alias:pvscript.pyIn detail, peeve will:Search for arequirements.txtCreate a virtual environment, if requiredUpdate the installed packages, if requiredActivate the virtual environment, if requiredRun the scriptAlternatively, you can addfrompeeveimportbootstrapbefore any third-party import and run the script the usual way:pythonscript.pyMotivationIn our team we often provide Python scripts with our internal Git repositories
to automate any kind of workflow.
As soon as the scripts rely on third-party packages, things start to get complicated.We all know that you shouldn't install Python packages globally.
However, creating virtual environment, activating it, and keeping the dependencies
up to date are a few steps too many if you just want to run a script.
You can easily forget to update the dependencies after agit pull,
or to activate the virtual environment when you start a new shell session.
Even worse, the commands for activation depend on your shell and operating system.While there are excellent tools like pipx, they typically require you to package
your scripts.
Especially for team members who are not Python experts because they develop in
other languages are have other roles such as testers, this situation is challenging.Installationpeeve is available on the Python Package Index:pipinstallpeeveSince peeve does not have any third-party dependencies, a global installation
is probably less harmful than usually.
Still, you should prefer pipx if you only need the command line usage:pipxinstallpeeveSince peeve is a single file with no dependencies, you can easily copy it into
you project ("vendoring") or use a similar mechanism like Git subtrees or Git submodules.UsageCommand lineAssuming you have a project with the following files:script.py
requirements.txt
...Usepeeveinstead ofpythonto run your script:peevescript.pyYou may also use the shorterpvalias:pvscript.pyIf thepeevecommand is not accesible, use:python-mpeevescript.pyIt will:Create a virtual environment.venv, if not available.Install therequirements.txt, if not up to date.Pass the remaining arguments to the python interpreter of the venv.If there is nothing to do, the startup is not much slower than the Python interpreter itself.APIAdd a single import line to your script:# standard library importsfrompeeveimportbootstrap# third-party importsThen you can execute the script directly with the Python interpreter:pythonscript.pyYou may need to replacepythonby the appropriate command on your system.
In any case, Python must be able to importpeeve, so you must have it installed
globally, inside the current virtual environment, or have thepeeve.pyscript on
the Python search path.AdvancedPython versionpeeve uses the Python interpreter it was invoked with.
You can combine the Python launcher with peeve to select the desired Python version:py-3.10-mpeevescript.pyCurrently, peeve does not check the Python version in any way.
If you call peeve with a different Pythin version than the one you used to create
the virtual environment, you may encounter problems.
In this case, delete the virtual environment and let peeve recreate it.Compatibility with pip and venvpeeve uses pip and venv and is fully compatible to direct usage of those tools.
This means that peeve can update and activate manually created virtual
envionments, or that you can manually modify virtual environments created with
peeve. However, peeve cannot detect such changes since they pypass the
hashing mechanism.peeve makes to assumptions about your project:you have arequirements.txtat the root of the projectyou want to use a virtual environment.venvat the root of the projectOther requirement files or virtual environments are ignored.Active virtual environmentsYou may use peeve from within an active virtual environment.
If the active environment is the desired.venvone, peeve will continue to use it.
If it is a different one, peeve will switch to.venvautomatically.FeaturesImplemented:Single file, no external dependenciesNo configuration requiredNo packaging of scripts requiredDual usage: from command line or as importDicovery ofrequirements.txtand existing.venvin parent directoryHashing of requirements to skip unnecessary upgradeFast in-process venv activation and script executionPlanned:Awareness of parallel Python versions and/or operating systemsSupport for other interpreter modes (e.g.-m,-cor interactive)Configuration of requirements file location and/or venv locationSupport for other dependency specification formatsAlternativesToxtoxis a much more powerful tool that can achieve a similar effect
if you invoketox -- script.pywith the followingtox.ini:[testenv]skip_install=truedeps=-r requirements.txtcommands=python {posargs}peeveis faster and works without any configuration.TODOGenerate code coverage reportGenerate HTML documentationSetup CIChangelogv0.2.0 (2022-04-23)Fix broken packagingFix Linux/MacOS supportFix Python 3.7 supportv0.1.0 (2022-04-23)Initial release