tzlocal: Local Time Zone Information for Python

raw JSON →
5.3.1 verified Tue May 12 auth: no python install: verified quickstart: verified

A Python module that provides the local time zone information as a tzinfo object. Current version: 5.3.1, released on March 5, 2025. Maintained under the MIT License. Requires Python 3.9 or higher. Release cadence: approximately every 6 months.

pip install tzlocal
error ModuleNotFoundError: No module named 'tzlocal'
cause The 'tzlocal' package is not installed in the current Python environment.
fix
Install the package using pip: pip install tzlocal
error ERROR: Package 'tzlocal' requires a different Python version: X.Y.Z. Expected '>=3.9'
cause Attempting to install or use `tzlocal` version 5.3.1 (which requires Python 3.9 or higher) with an older Python interpreter.
fix
Upgrade your Python interpreter to version 3.9 or higher, or use an older tzlocal version compatible with your Python (though this is not recommended).
error AttributeError: 'ZoneInfo' object has no attribute 'localize'
cause This error occurs when trying to use the `localize` method (common with `pytz` timezone objects) on a `zoneinfo.ZoneInfo` object, which is returned by `tzlocal.get_localzone()` from version 4.0 onwards. `zoneinfo` objects do not have a `localize` method.
fix
Use datetime.datetime.replace() or datetime.datetime.astimezone() to make a naive datetime object timezone-aware:
import datetime
from tzlocal import get_localzone

local_tz = get_localzone()
naive_dt = datetime.datetime.now() # naive datetime
aware_dt = naive_dt.replace(tzinfo=local_tz)
# Or, to convert an already aware datetime from one zone to another:
# dt_utc = datetime.datetime.now(datetime.timezone.utc)
# aware_dt_local = dt_utc.astimezone(local_tz)
error ModuleNotFoundError: No module named 'tzlocal.get_localzone'
cause The `get_localzone` function is directly available under the `tzlocal` module, not as a submodule.
fix
Import the function directly from the tzlocal module:
from tzlocal import get_localzone
breaking Version 5.0 removed pytz_deprecation_shim, returning only zoneinfo objects. For pytz compatibility, use version 4.0.
fix Downgrade to version 4.0 for pytz compatibility.
gotcha get_localzone_name() may raise an error if no time zone name is configured on Unix systems. Use get_localzone() if only the tzinfo object is needed.
fix Use get_localzone() when time zone name is unavailable.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.04s 17.9M
3.10 slim (glibc) - - 0.04s 18M
3.11 alpine (musl) - - 0.07s 19.7M
3.11 slim (glibc) - - 0.06s 20M
3.12 alpine (musl) - - 0.05s 11.6M
3.12 slim (glibc) - - 0.05s 12M
3.13 alpine (musl) - - 0.06s 11.2M
3.13 slim (glibc) - - 0.05s 12M
3.9 alpine (musl) - - 0.03s 17.4M
3.9 slim (glibc) - - 0.03s 18M

Retrieve and print the local time zone information.

from tzlocal import get_localzone

local_timezone = get_localzone()
print(local_timezone)