absl-py is a collection of Python library code for building Python applications, originating from Google's internal codebase. It provides essential utilities for application startup, a distributed command-line flags system, a custom logging module, and testing utilities. The library is actively maintained, with frequent releases, and is currently at version 2.4.0.
Install & Compatibility
Where this runs
tested against v2.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.925 runs
installs and imports cleanly · install 0.0s · import 0.265s · 18.7MB
glibcpy 3.10–3.925 runs
installs and imports cleanly · install 1.6s · import 0.222s · 19MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
app
✓ from absl import app
Used for managing application lifecycle and parsing command-line flags. The main entry point is typically `app.run(main_function)`.
flags
✓ from absl import flags
Provides functions like `flags.DEFINE_string`, `flags.DEFINE_integer`, and `flags.FLAGS` to define and access command-line flags.
logging
✓ from absl import logging
Offers a custom logging module built on top of Python's standard logging, with features like various verbosity levels and conditional logging.
absltest
✓ from absl.testing import absltest
The base class for Abseil-style Python tests, similar to `unittest.TestCase`.
flagsaver
✓ from absl.testing import flagsaver
A utility for testing code that relies on `absl.flags`, allowing flags to be temporarily overridden for test isolation.
This example demonstrates defining and using command-line flags, basic logging, and integrating with `absl.app.run()` to handle program execution. You can run this script and pass flags like `--greeting_target=Pythonista` or `--repeat_count=5`. The `os.environ` usage is for demonstrating how to programmatically set flags, especially useful in testing contexts where direct command-line arguments might not be feasible.
from absl import app
from absl import flags
from absl import logging
import os
FLAGS = flags.FLAGS
flags.DEFINE_string('greeting_target', 'World', 'The entity to greet.')
flags.DEFINE_integer('repeat_count', 1, 'How many times to repeat the greeting.', lower_bound=1)
flags.DEFINE_boolean('verbose', False, 'If true, print verbose output.')
def main(argv):
if FLAGS.verbose:
logging.set_verbosity(logging.DEBUG)
logging.debug('Verbose mode enabled. Non-flag arguments: %s', argv[1:])
else:
logging.set_verbosity(logging.INFO)
for _ in range(FLAGS.repeat_count):
logging.info('Hello, %s!', FLAGS.greeting_target)
# Example of an argument passed directly, not as a flag
if len(argv) > 1:
logging.warning('Unrecognized positional arguments: %s', argv[1:])
if __name__ == '__main__':
# To run: python your_script.py --greeting_target=User --repeat_count=3 --verbose
# Or with environment variable for testing:
# os.environ['greeting_target'] = 'TestUser'
# os.environ['repeat_count'] = '2'
# Then run: python your_script.py --verbose
app.run(main)
Debug
Known issues
breakingAbseil Python has progressively dropped support for older Python versions. Version 2.4.0 dropped support for Python 3.8 and 3.9. Version 2.2.2 removed support for Python 3.7. Version 2.0.0 dropped support for Python 3.6.fixEnsure your Python environment is version 3.10 or newer to use the latest `absl-py` versions.
affects: <2.4.0 (for Python 3.8/3.9), <2.2.2 (for Python 3.7), <2.0.0 (for Python 3.6)
gotchaDirectly evaluating `absl.flags.Flag` instances in a boolean context (e.g., `if FLAGS.myflag:`) will raise an error. This prevents accidental usage of the Flag object itself rather than its value.fixAlways access the flag's value using the `.value` attribute: `if FLAGS.myflag.value:`.
affects: >=1.1.0
breakingThe `absl.logging.exception` function's behavior regarding the `exc_info` argument changed in v2.0.0. Previously, passing `exc_info` could raise a `KeyError`. It now correctly accepts `exc_info` as an argument (with a default value of `True`).fixIf your code explicitly passes `exc_info` to `logging.exception`, ensure `absl-py` is version 2.0.0 or higher. Otherwise, verify behavior on older versions if `exc_info` is relied upon.
affects: <2.0.0
gotchaFlag names defined with `absl.flags.DEFINE_*` are globally registered. This means that if multiple modules or libraries define flags with the same name, a `DuplicateFlagError` will occur upon import or flag definition.fixChoose unique and descriptive flag names, especially in larger applications or when integrating multiple libraries. For more advanced isolation, consider managing flags with separate `flags.FlagValues` instances, though this is less common for standard usage.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'absl'
The 'absl-py' package is not installed in the Python environment.
fixInstall the package using 'pip install absl-py'.
ImportError: No module named absl.flags
The 'absl-py' package is not installed or is installed in a different Python environment.
fixEnsure 'absl-py' is installed in the correct environment using 'pip install absl-py'.
ImportError: No module named absl.testing
The 'absl-py' package is not installed or is installed in a different Python environment.
fixEnsure 'absl-py' is installed in the correct environment using 'pip install absl-py'.
ImportError: No module named 'absl'
The 'absl-py' package is not installed or is installed in a different Python environment.
fixEnsure 'absl-py' is installed in the correct environment using 'pip install absl-py'.
absl.flags._exceptions.UnparsedFlagAccessError: Trying to access flag --[flag_name] before flags were parsed.
A flag defined using `absl.flags` is being accessed before `absl.app.run()` has been called or before `absl.flags.FLAGS(sys.argv)` has explicitly parsed the command-line arguments.
fixEnsure `app.run(main)` is called at the entry point of your script, or explicitly parse flags with `FLAGS(sys.argv)` before accessing them. Example: `from absl import app, flags; FLAGS = flags.FLAGS; flags.DEFINE_string('my_flag', 'default', 'My flag.'); def main(argv): print(FLAGS.my_flag); if __name__ == '__main__': app.run(main)` Audit
Dependencies
No dependency data recorded yet.