Crate Configuration

Build Artifacts for pyembed

The pyembed crate needs to reference special artifacts as part of its build process in order to compile a Python interpreter into a binary. The most important of these artifacts is a library providing Python symbols.

By default, the pyembed crate’s build.rs build script will run pyoxidizer run-build-script, which will attempt to find a PyOxidizer config file and evaluate its default build script target. Using environment variables set by cargo and by the build.rs script, artifacts will be placed in the correct locations and pyembed will be built seemingly like any normal Rust crate.

The special build artifacts are generated by resolving a configuration file target returning a PythonEmbeddedResources instance. In the auto-generated configuration file, the embedded target returns such a type.

Cargo Features to Control Building

The pyembed crate and generated Rust projects share a set of build-mode-* Cargo feature flags to control how build artifacts are created and consumed.

The features are described in the following sections.

build-mode-default

This is the default build mode. It is enabled by default.

This build mode uses default Python linking behavior and feature detection as implemented by the cpython and python3-sys crates. It will attempt to find a python in PATH or from the PYTHON_SYS_EXECUTABLE environment variable and dynamically link against it.

This is the default mode for convenience, as it enables the pyembed crate to build in the most environments. However, the built binaries will have a dependency against a foreign libpython and likely aren’t suitable for distribution.

build-mode-standalone

Do not attempt to invoke pyoxidizer or find artifacts it would have built. It is possible to build the pyembed crate in this mode if the rust-cpython and python3-sys crates can find a Python interpreter. But, the pyembed crate may not be usable or work in the way you want it to.

This mode is intended to be used for performing quick testing on the pyembed crate. It is quite possible that linking errors will occur in this mode unless you take additional actions to point Cargo at appropriate libraries.

build-mode-pyoxidizer-exe

A pyoxidizer executable will be run to generate build artifacts.

The path to this executable can be defined via the PYOXIDIZER_EXE environment variable. Otherwise PATH will be used.

At build time, pyoxidizer run-build-script will be run. A PyOxidizer configuration file will be discovered using the heuristics described at Automatic File Location Strategy. OUT_DIR will be set if running from cargo, so a pyoxidizer.bzl next to the main Rust project being built should be found and used.

pyoxidizer run-build-script will resolve the default build script target by default. To override which target should be resolved, specify the target name via the PYOXIDIZER_BUILD_TARGET environment variable. e.g.:

$ PYOXIDIZER_BUILD_TARGET=build-artifacts cargo build

build-mode-prebuilt-artifacts

This mode tells the build script to reuse artifacts that were already built. (Perhaps you called pyoxidizer build or pyoxidizer run-build-script outside the context of a normal cargo build.)

In this mode, the build script will look for artifacts in the directory specified by PYOXIDIZER_ARTIFACT_DIR if set, falling back to OUT_DIR. This directory must have a cargo_metadata.txt file, which will be printed to stdout by the build script to tell Cargo how to link a Python library.