pip-run (2024)

pip-run (1)pip-run (2)pip-run (3)pip-run (4)pip-run (5)pip-run (6)pip-run (7)

pip-run provides on-demand temporary package installationfor a single execution run.

It replaces this series of commands (or their Windows equivalent):

$ virtualenv --python pythonX.X --system-site-packages $temp/env$ $temp/env/bin/pip install pkg1 pkg2 -r reqs.txt$ $temp/env/bin/python ...$ rm -rf $temp/env

With this single-line command:

$ py -X.X -m pip-run pkg1 pkg2 -r reqs.txt -- ...

Features include

  • Downloads missing dependencies and makes their packages available for import.

  • Installs packages to a special staging location such that they’re not installed after the process exits.

  • Relies on pip to cache downloads of such packages for reuse.

  • Leaves no trace of its invocation (except files in pip’s cache).

  • Supersedes installed packages when required.

  • Re-uses the pip tool chain for package installation.

pip-run is not intended to solve production dependency management, but does aim to address the other, one-off scenarios around dependency management:

  • trials and experiments

  • build setup

  • test runners

  • just in time script running

  • interactive development

  • bug triage

pip-run is a complement to Pip and Virtualenv, intended to morereadily address the on-demand needs.

Installation

pip-run is meant to be installed in the system site packagesalongside pip, though it can also be installed in a virtualenv.

Usage

  • as script launcher

  • as runtime dependency context manager

  • as interactive interpreter in dependency context

  • as module launcher (akin to python -m)

  • as a shell shebang (#!/usr/bin/env pip-run), to create single-file Python tools

Invoke pip-run from the command-line using the console entryscript (simply pip-run) or using the module executable (python -m pip-run). This latter usage is particularly convenientfor testing a command across various Python versions.

Parameters following pip-run are passed directly to pip install,so pip-run numpy will install numpy (reporting any work doneduring the install) and pip-run -v -r requirements.txt will verboselyinstall all the requirements listed in a file called requirements.txt(quiet is the default).Any environment variables honored by pipare also honored.

Following the parameters to pip install, one may optionallyinclude a -- after which any parameters will be executedby a Python interpreter in the context or directly if prefixed by!.

See pip-run --help for more details.

Examples

The examples folder in this projectincludes some examples demonstratingthe power and usefulness of the project. Read the docs on those examplesfor instructions.

Module Script Runner

Perhaps the most powerful usage of pip-run is its ability to invokeexecutable modules and packages viarunpy (akapython -m):

$ pip-run cowsay -- -m cowsay "moove over, pip-run" -------------------< moove over, pip-run > ------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
pip-run (8)

Module Executable Runner

Some package tools, like ranger, areinvoked with a unique executable instead of a module. pip-run canrun an executable from a package if it is prependend by a !:

$ pip-run ranger-fm -- '!ranger'

Command Runner

Note that everything after the – is passed to the python invocation,so it’s possible to have a one-liner that runs under a dependencycontext:

$ python -m pip-run requests -- -c "import requests; print(requests.get('https://pypi.org/project/pip-run').status_code)"200

As long as pip-run is installed in each of Python environmentson the system, this command can be readily repeated on the otherpython environments by specifying the relevant interpreter:

$ py -3.7 -m pip-run ...

Script Runner

pip-run can run a Python file with indicated dependencies. Becausearguments after -- are passed directly to the Python interpreterand because the Python interpreter will run any script, invoking a scriptwith dependencies is easy. Consider this script “myscript.py”:

#!/usr/bin/env pythonimport requestsreq = requests.get('https://pypi.org/project/pip-run')print(req.status_code)

To invoke it while making sure requests is present:

$ pip-run requests – myscript.py

pip-run will make sure that requests is installed then invokethe script in a Python interpreter configured with requests and itsdependencies.

For added convenience when running scripts, pip-run will inferthe beginning of Python parameters if it encounters a filenameof a Python script that exists, allowing for omission of the --for script invocation:

$ pip-run requests myscript.py

Script-declared Dependencies

Building on Script Runner above, pip-run also allowsdependencies to be declared in the script itself so thatthe user need not specify them at each invocation.

To declare dependencies in a script, add a __requires__variable or # Requirements: section to the script:

#!/usr/bin/env python__requires__ = ['requests']# or (PEP 723)# /// script# dependencies = ['requests']# ///import requestsreq = requests.get('https://pypi.org/project/pip-run')print(req.status_code)

With that declaration in place, one can now invoke pip-run withoutdeclaring any parameters to pip:

$ pip-run myscript.py200

The format for requirements must follow PEP 508.

Single-script Tools and Shebang Support

Combined with in-script dependencies, pip-run can be used as a shebang tocreate fully self-contained scripts that install and run their owndependencies, as long as pip-run is installed on the system PATH.Consider, for example, the pydragon script:

#!/usr/bin/env pip-run__requires__ = ['requests', 'beautifulsoup4', 'cowsay']import requestsfrom bs4 import BeautifulSoup as BSimport cowsayres = requests.get('https://python.org')b = BS(res.text, 'html.parser')cowsay.dragon(b.find("div", class_="introduction").get_text())

This executable script is available in the repo as examples/pydragon (forUnix) and examples/pydragon.py (for Windows [2]). Executing this script isequivalent to executing pip-run pydragon.

By default, the script will assemble the dependencies on each invocation,which may be inconvenient for a script. See Environment Persistence for a technique to persist the assembleddependencies across invocations. One may inject PIP_RUN_MODE=persistin the shebang, but be aware that doing so breaks Windows portability.

Other Script Directives

pip-run also recognizes a global __index_url__ attribute. If present,this value will supply --index-url to pip with the attribute value,allowing a script to specify a custom package index:

#!/usr/bin/env python__requires__ = ['my_private_package']__index_url__ = 'https://my.private.index/'import my_private_package...

Extracting Requirements

After having used pip-run to run scripts, it may be desirable to extract the requirements from the __requires__ variable or # Requirements: section of ascript to install those more permanently. pip-run provides a routine to facilitatethis case:

$ py -m pip_run.read-deps examples/pydragonrequests beautifulsoup4 cowsay

On Unix, it is possible to pipe this result directly to pip:

$ pip install $(py -m pip_run.read-deps examples/pydragon)

To generate a requirements.txt file, specify a newline separator:

$ py -m pip_run.read-deps --separator newline examples/pydragon > requirements.txt

And since pipenv uses the same syntax,the same technique works for pipenv:

$ pipenv install $(python -m pip_run.read-deps script.py)

Interactive Interpreter

pip-run also offers a painless way to run a Python interactiveinterpreter in the context of certain dependencies:

$ /clean-install/python -m pip-run boto>>> import boto>>>

Experiments and Testing

Because pip-run provides a single-command invocation, itis great for experiments and rapid testing of various packagespecifications.

Consider a scenario in which one wishes to create an environmentwhere two different versions of the same package are installed,such as to replicate a broken real-world environment. Stack twoinvocations of pip-run to get two different versions installed:

$ pip-run keyring==21.8.0 -- -m pip-run keyring==22.0.0 -- -c "import importlib.metadata, pprint; pprint.pprint([dist._path for dist in importlib.metadata.distributions() if dist.metadata['name'] == 'keyring'])"[PosixPath('/var/folders/03/7l0ffypn50b83bp0bt07xcch00n8zm/T/pip-run-a3xvd267/keyring-22.0.0.dist-info'),PosixPath('/var/folders/03/7l0ffypn50b83bp0bt07xcch00n8zm/T/pip-run-1fdjsgfs/keyring-21.8.0.dist-info')]

IPython Inference

If IPython is specified as one of the dependencies, the Pythoninterpreter will be launched via IPython (using -m IPython)for interactive mode. This behaviour may be toggled off bysetting the environment variable PIP_RUN_IPYTHON_MODE=ignore.

How Does It Work

pip-run effectively does the following:

  • pip install -t $TMPDIR

  • PYTHONPATH=$TMPDIR python

  • cleanup

For specifics, see pip_run.run().

Environment Persistence

pip-run honors the PIP_RUN_RETENTION_STRATEGY variable. If unset orset to destroy, dependencies are installed to a temporary directory oneach invocation (and deleted after). Setting this variable to persist willinstead create or re-use a directory in the user’s cache, only installing thedependencies if the directory doesn’t already exist. A separate cache ismaintained for each combination of requirements specified.

persist strategy can greatly improve startup performance at the expense ofstaleness and accumulated cruft.

Without PIP_RUN_RETENTION_STRATEGY=persist (or with =destroy),pip-run will re-install dependencies every time a script runs, silentlyadding to the startup time while dependencies are installed into an ephemeralenvironment, depending on how many dependencies there are and whether thedependencies have been previously downloaded to the local pip cache. Usepip-run -v ... to see the installation activity.

The location of the cache can be revealed with this command:

py -c 'import importlib; print(importlib.import_module("pip_run.retention.persist").paths.user_cache_path)'

Limitations

  • Due to limitations with pip, pip-run cannot run with “editable”(-e) requirements.

  • pip-run uses a sitecustomize module to ensure that .pth filesin the requirements are installed. As a result, any environmentthat has a sitecustomize module will find that module maskedwhen running under pip-run.

Comparison with pipx

The pipx project is another matureproject with similar goals. Both projects expose a project and itsdependencies in ephemeral environments. The main difference is pipxprimarily exposes Python binaries (console scripts) from thoseenvironments whereas pip-run exposes a Python context (includingrunpy scripts).

Feature

pip-run

pipx

user-mode operation

invoke console scripts

invoke runpy modules

run standalone scripts

interactive interpreter with deps

ephemeral environments

persistent environments

PEP 582 support

Specify optional dependencies

Python 2 support

Comparison with virtualenvwrapper mktmpenv

The virtualenvwrapper projectattempts to address some of the use-cases that pip-run solves,especially with the mktmpenv command, which destroys thevirtualenv after deactivation. The main difference is that pip-runis transient only for the invocation of a single command, whilemktmpenv lasts for a session.

Feature

pip-run

mktmpenv

create temporary package environment

re-usable across python invocations

portable

one-line invocation

multiple interpreters in session

run standalone scripts

interactive interpreter with deps

re-use existing environment

ephemeral environments

persistent environments

Integration

The author created this package with the intention of demonstratingthe capability before integrating it directly with pip in a commandsuch as pip run. After proposing the change, the idea was largelyrejected in pip 3971.

If you would like to see this functionality made available in pip,please upvote or comment in that ticket.

Versioning

pip-run uses semver, so you can use this library withconfidence about the stability of the interface, evenduring periods of great flux.

Testing

Invoke tests with tox.

For Enterprise

Available as part of the Tidelift Subscription.

This project and the maintainers of thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use.

Learn more.

pip-run (2024)
Top Articles
How To Buy Dogecoin (DOGE)
Tata AIG Life is now Tata AIA Life Insurance
Drury Inn & Suites Bowling Green
Kem Minnick Playboy
Obor Guide Osrs
Tesla Supercharger La Crosse Photos
Arkansas Gazette Sudoku
What to Serve with Lasagna (80+ side dishes and wine pairings)
877-668-5260 | 18776685260 - Robocaller Warning!
Chuckwagon racing 101: why it's OK to ask what a wheeler is | CBC News
CA Kapil 🇦🇪 Talreja Dubai on LinkedIn: #businessethics #audit #pwc #evergrande #talrejaandtalreja #businesssetup…
Weekly Math Review Q4 3
Seafood Bucket Cajun Style Seafood Restaurant in South Salt Lake - Restaurant menu and reviews
General Info for Parents
Watch TV shows online - JustWatch
Miss America Voy Forum
Lima Funeral Home Bristol Ri Obituaries
Truck Trader Pennsylvania
Tvtv.us Duluth Mn
Mals Crazy Crab
Violent Night Showtimes Near Amc Fashion Valley 18
iZurvive DayZ & ARMA Map
Earl David Worden Military Service
Ratchet & Clank Future: Tools of Destruction
Graphic Look Inside Jeffrey Dahmer
Aerocareusa Hmebillpay Com
Cincinnati Adult Search
Rochester Ny Missed Connections
Chamberlain College of Nursing | Tuition & Acceptance Rates 2024
Craigslist Lake Charles
Spectrum Outage in Queens, New York
Aes Salt Lake City Showdown
Ihs Hockey Systems
Craigslist Boerne Tx
Mercedes W204 Belt Diagram
Bridger Park Community Garden
Smith And Wesson Nra Instructor Discount
3496 W Little League Dr San Bernardino Ca 92407
Tripadvisor Vancouver Restaurants
Payrollservers.us Webclock
Natasha Tosini Bikini
Bf273-11K-Cl
Theater X Orange Heights Florida
Jimmy John's Near Me Open
Helpers Needed At Once Bug Fables
Skyward Login Wylie Isd
Samantha Lyne Wikipedia
Tweedehands camper te koop - camper occasion kopen
Psalm 46 New International Version
Bob Wright Yukon Accident
Texas Lottery Daily 4 Winning Numbers
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6361

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.