Shiv API

cli

shiv.cli.copy_bootstrap(bootstrap_target: pathlib.Path) → None[source]

Copy bootstrap code from shiv into the pyz.

This function is excluded from type checking due to the conditional import.

Parameters:bootstrap_target – The temporary directory where we are staging pyz contents.
shiv.cli.find_entry_point(site_packages: pathlib.Path, console_script: str) → str[source]

Find a console_script in a site-packages directory.

Console script metadata is stored in entry_points.txt per setuptools convention. This function searches all entry_points.txt files and returns the import string for a given console_script argument.

Parameters:
  • site_packages – A path to a site-packages directory on disk.
  • console_script – A console_script string.

constants —

This module contains various error messages.

builder

This module is a slightly modified implementation of Python’s “zipapp” module.

We’ve copied a lot of zipapp’s code here in order to backport support for compression. https://docs.python.org/3.7/library/zipapp.html#cmdoption-zipapp-c

shiv.builder.create_archive(source: pathlib.Path, target: pathlib.Path, interpreter: str, main: str, compressed: bool = True) → None[source]

Create an application archive from SOURCE.

A slightly modified version of stdlib’s zipapp.create_archive

shiv.builder.write_file_prefix(f: IO[Any], interpreter: str) → None[source]

Write a shebang line.

Parameters:
  • f – An open file handle.
  • interpreter – A path to a python interpreter.

pip

shiv.pip.clean_pip_env() → Generator[[None, None], None][source]

A context manager for temporarily removing ‘PIP_REQUIRE_VIRTUALENV’ from the environment.

Since shiv installs via –target, we need to ignore venv requirements if they exist.

shiv.pip.install(args: List[str]) → None[source]

pip install as a function.

Accepts a list of pip arguments.

>>> install(['numpy', '--target', 'site-packages'])
Collecting numpy
Downloading numpy-1.13.3-cp35-cp35m-manylinux1_x86_64.whl (16.9MB)
    100% || 16.9MB 53kB/s
Installing collected packages: numpy
Successfully installed numpy-1.13.3

bootstrap

shiv.bootstrap.bootstrap()[source]

Actually bootstrap our shiv environment.

shiv.bootstrap.cache_path(archive, root_dir, build_id)[source]

Returns a ~/.shiv cache directory for unzipping site-packages during bootstrap.

Parameters:
  • archive (ZipFile) – The zipfile object we are bootstrapping from.
  • buidl_id (str) – The build id generated at zip creation.
shiv.bootstrap.current_zipfile()[source]

A function to vend the current zipfile, if any

shiv.bootstrap.extract_site_packages(archive, target_path, compile_pyc, compile_workers=0)[source]

Extract everything in site-packages to a specified path.

Parameters:
  • archive (ZipFile) – The zipfile object we are bootstrapping from.
  • target_path (Path) – The path to extract our zip to.
shiv.bootstrap.import_string(import_name)[source]

Returns a callable for a given setuptools style import string

Parameters:import_name – A console_scripts style import string

bootstrap.environment

This module contains the Environment object, which combines settings decided at build time with overrides defined at runtime (via environment variables).

bootstrap.interpreter

The code in this module is adapted from https://github.com/pantsbuild/pex/blob/master/pex/pex.py

It is used to enter an interactive interpreter session from an executable created with shiv.