Main functions¶
The names that can be imported from feu. They are imported lazily, on first use.
feu ¶
Root package of feu.
feu is a lightweight library to manage Python packages and their
versions across Python environments. The only required dependency is
packaging. The optional libraries (click, gitpython,
requests and rich) are imported lazily, and a feature that
needs one of them raises feu.imports.MissingPackageError if it is
missing.
The main functions and classes are available from this package:
is_package_availableandis_module_available: check if a package or a module can be used.get_package_versionandcompare_version: read the installed version of a package and compare it to a version.Targetandresolve_target: the environment (Python version, free-threaded flag, OS and CPU architecture) a compatibility answer is about.is_valid_versionandfind_closest_version: check a version request against a target, or find the closest valid version. A version request is an exact version ("2.4.0"), a wildcard ("2.4.*") or""for the latest version.install_packageandinstall_package_closest_version: install a package, as written or at the closest valid version.InstallerSpecandPackageSpecdescribe the installer and the package.
The public names are imported lazily (PEP 562): import feu imports
only feu.imports, so it does not import any optional library. from
feu import compare_version imports feu.version the first time it
is used.
The subpackages are feu.compat (compatibility registry and discovery
from PyPI), feu.install (installers and dependency resolvers),
feu.version (version utilities, PyPI and pyproject.toml),
feu.imports (optional dependencies), feu.utils (shared helpers),
feu.benchmark (trend of pytest-benchmark results),
feu.github (GitHub API), feu.local_git (tags of a git
repository) and feu.testing (pytest marks).
Example
>>> from feu import is_package_available
>>> from feu.compat import Target, find_closest_version
>>> is_package_available("packaging")
True
>>> find_closest_version("numpy", "2.3.0", Target(python_version="3.9"))
'2.0.2'
feu.InstallerSpec
dataclass
¶
Define a dataclass to represent an installer specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The installer name. |
required |
arguments
|
str | Sequence[str]
|
The optional installer arguments. A string is
split once with |
()
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
>>> from feu.install import InstallerSpec
>>> installer1 = InstallerSpec("pip")
>>> installer1
InstallerSpec(name='pip', arguments=())
>>> installer2 = InstallerSpec("pip", arguments="-U --pre")
>>> installer2
InstallerSpec(name='pip', arguments=('-U', '--pre'))
>>> installer3 = InstallerSpec("pip", arguments=["-U", "--pre"])
>>> installer3 == installer2
True
feu.PackageSpec
dataclass
¶
Define a dataclass to represent a package specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The package name. |
required |
version
|
str | None
|
The package version request: an exact version
( |
''
|
extras
|
Sequence[str] | None
|
Optional package extra dependencies. A list is stored as a tuple. |
()
|
Example
>>> from feu.utils.package import PackageSpec
>>> pkg1 = PackageSpec("my_package")
>>> pkg1
PackageSpec(name='my_package', version='', extras=())
>>> str(pkg1)
'my_package'
>>> pkg2 = PackageSpec("my_package", version="1.2.3")
>>> pkg2
PackageSpec(name='my_package', version='1.2.3', extras=())
>>> str(pkg2)
'my_package==1.2.3'
>>> pkg3 = PackageSpec("my_package", version="1.*", extras=["security", "socks"])
>>> pkg3
PackageSpec(name='my_package', version='1.*', extras=('security', 'socks'))
>>> str(pkg3)
'my_package[security,socks]==1.*'
feu.PackageSpec.to_requirement ¶
to_requirement() -> Requirement
Convert to a packaging.requirements.Requirement.
Returns:
| Type | Description |
|---|---|
Requirement
|
The current package as a requirement. |
Example
>>> from feu.utils.package import PackageSpec
>>> PackageSpec("my_package", version="1.2.*").to_requirement()
<Requirement('my_package==1.2.*')>
feu.PackageSpec.with_version ¶
with_version(version: str | None) -> PackageSpec
Create a new PackageSpec instance with the given version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str | None
|
The new version request to apply. |
required |
Returns:
| Type | Description |
|---|---|
PackageSpec
|
A new instance of PackageSpec with the updated version. |
Example
>>> from feu.utils.package import PackageSpec
>>> pkg = PackageSpec("my_package", version="1.2.0")
>>> pkg
PackageSpec(name='my_package', version='1.2.0', extras=())
>>> pkg2 = pkg.with_version("1.2.3")
>>> pkg2
PackageSpec(name='my_package', version='1.2.3', extras=())
feu.Target
dataclass
¶
Identify the environment a package compatibility constraint applies to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
python_version
|
str
|
The Python version, e.g. |
required |
free_threaded
|
bool
|
|
False
|
os
|
str | None
|
The operating system, e.g. |
None
|
arch
|
str | None
|
The CPU architecture, e.g. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Example
>>> from feu.compat.target import Target
>>> Target(python_version="3.14", free_threaded=True, os="linux", arch="x86_64")
Target(python_version='3.14', free_threaded=True, os='linux', arch='x86_64')
feu.compare_version ¶
compare_version(
package: str, op: Callable, version: str
) -> bool
Compare the installed version of a package to a given version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package
|
str
|
The package to check. |
required |
op
|
Callable
|
The comparison operator, e.g. |
required |
version
|
str
|
The version to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The comparison status. |
Raises:
| Type | Description |
|---|---|
InvalidVersion
|
If the package is installed
and |
Example
>>> import operator
>>> from feu.version import compare_version
>>> compare_version("pytest", op=operator.ge, version="7.3.0")
True
feu.find_closest_version ¶
find_closest_version(
pkg_name: str,
pkg_version: str | None,
target: Target,
*,
registry: CompatRegistry | None = None,
) -> str
Find the closest valid version for a package using the default registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg_name
|
str
|
The package name to check (e.g., |
required |
pkg_version
|
str | None
|
The requested package version: an exact
version, a wildcard (e.g. |
required |
target
|
Target
|
The compatibility target. |
required |
registry
|
CompatRegistry | None
|
The compatibility registry. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The closest valid version as a string. An exact version is
returned unchanged if it is valid or if the package is not
configured for the target, otherwise the nearest range
boundary is returned. A wildcard or empty request is
resolved to the latest compatible version, or returned
unchanged if it lies in an open-ended range or if the
registry has no data for the package (see
|
Raises:
| Type | Description |
|---|---|
UnsupportedVersionError
|
If no package version is valid for the given target, or if no known version satisfies a wildcard request. |
ValueError
|
If |
RuntimeError
|
If the versions needed to match a wildcard cannot be downloaded from PyPI. |
MissingPackageError
|
If a wildcard needs a download and
|
Example
>>> from feu.compat import find_closest_version, Target
>>> find_closest_version(
... pkg_name="numpy", pkg_version="2.0.2", target=Target(python_version="3.11")
... )
'2.0.2'
feu.get_package_version ¶
get_package_version(package: str) -> Version | None
Get the package version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package
|
str
|
The package name. |
required |
Returns:
| Type | Description |
|---|---|
Version | None
|
The package version, or |
Example
>>> from feu.version import get_package_version
>>> get_package_version("pytest")
<Version('...')>
feu.install_package ¶
install_package(
installer: InstallerSpec,
package: PackageSpec,
*,
registry: Registry[type[Installer]] | None = None,
) -> None
Install a package with the specified installer.
The package version is installed as specified, without any version
compatibility check. The dependency resolver of the package can add
requirements, e.g. numpy<2.0.0 for an old pandas (see
feu.install.resolver). Use install_package_closest_version
to install the closest compatible version. The cached availability
checks (feu.imports.clear_availability_caches) are cleared after
a successful installation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
installer
|
InstallerSpec
|
The installer specification. |
required |
package
|
PackageSpec
|
The package specification. |
required |
registry
|
Registry[type[Installer]] | None
|
The installer registry. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no installer is registered for the installer name, or if the tool of the installer is not available. |
ValueError
|
If the package specification is not a valid requirement (e.g. a malformed version). |
CalledProcessError
|
If the installation command fails. |
Example
>>> from feu.install import InstallerSpec, install_package
>>> from feu.utils.package import PackageSpec
>>> install_package(
... installer=InstallerSpec("pip"), package=PackageSpec(name="pandas", version="2.2.2")
... ) # doctest: +SKIP
feu.install_package_closest_version ¶
install_package_closest_version(
installer: InstallerSpec,
package: PackageSpec,
*,
target: Target | None = None,
registry: Registry[type[Installer]] | None = None,
) -> None
Install a package and associated packages by using the specified installer.
This function finds the closest valid version if the specified version is not compatible with the target. By default, the target is the current environment: the Python version, the free-threaded flag, the OS and the CPU architecture.
The package version is a version request: an exact version
('2.0.2'), a wildcard ('2.*') or '' for the latest
version. A request is resolved to the latest compatible version. If
that version lies in an open-ended range, the request itself is
installed, so the installer picks the real latest release.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
installer
|
InstallerSpec
|
The installer specification. |
required |
package
|
PackageSpec
|
The package specification. |
required |
target
|
Target | None
|
The compatibility target. |
None
|
registry
|
Registry[type[Installer]] | None
|
The installer registry. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no installer is registered for the installer name, if the tool of the installer is not available, or if the versions needed to match a wildcard cannot be downloaded. |
MissingPackageError
|
If a wildcard needs a download and
|
UnsupportedVersionError
|
If no version of the package is valid for the target, or if no known version satisfies a wildcard request. |
ValueError
|
If the package version is not a valid version request. |
CalledProcessError
|
If the installation command fails. |
Example
>>> from feu.install import InstallerSpec, install_package_closest_version
>>> from feu.utils.package import PackageSpec
>>> install_package_closest_version(
... installer=InstallerSpec("pip"), package=PackageSpec(name="pandas", version="2.2.2")
... ) # doctest: +SKIP
feu.is_valid_version ¶
is_valid_version(
pkg_name: str,
pkg_version: str | None,
target: Target,
*,
registry: CompatRegistry | None = None,
) -> bool
Check if a package version is valid for a target using the default registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg_name
|
str
|
The package name to check (e.g., |
required |
pkg_version
|
str | None
|
The package version request to validate: an
exact version, a wildcard (e.g. |
required |
target
|
Target
|
The compatibility target. |
required |
registry
|
CompatRegistry | None
|
The compatibility registry. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
For an exact version: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If the versions needed to match a wildcard cannot be downloaded from PyPI. |
MissingPackageError
|
If a wildcard needs a download and
|
Example
>>> from feu.compat import is_valid_version, Target
>>> is_valid_version(
... pkg_name="numpy", pkg_version="2.0.2", target=Target(python_version="3.11")
... )
True
feu.resolve_target ¶
resolve_target(
python_version: str | None = None,
*,
free_threaded: bool | None = None,
) -> Target
Resolve the Target of an environment.
The platform (OS and CPU architecture) is always the current one. The Python version and the free-threaded flag can be chosen, e.g. by a CI matrix that checks other Python versions, and fall back to the current interpreter.
If python_version ends with t, the target is a
free-threaded build. In that case, free_threaded=False is
invalid because it contradicts the t suffix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
python_version
|
str | None
|
The Python version, e.g. |
None
|
free_threaded
|
bool | None
|
Whether the target is a free-threaded build.
If not provided, it is inferred from |
None
|
Returns:
| Type | Description |
|---|---|
Target
|
The resolved target, with the current OS and architecture. The
OS or the architecture is |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Example
>>> from feu.compat.target import resolve_target
>>> target = resolve_target(python_version="3.14t")
>>> target.python_version, target.free_threaded
('3.14', True)