Main functions¶
feu ¶
Root package of feu.
The public names are imported lazily (PEP 562): import feu does not
import any submodule, so it does not import any optional library. from
feu import compare_version imports feu.version the first time it
is used.
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. |
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. A wildcard or empty request is
resolved to the latest compatible version, or returned
unchanged if it lies in an open-ended range (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 |
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 is installed as specified, without any version
compatibility check. 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. |
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, or if the tool of the installer is not available. |
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
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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. |
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)