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 tzlocalVerified import paths — ran on the pinned version, not inferred.
Retrieve and print the local time zone information.
Downgrade to version 4.0 for pytz compatibility.
Use get_localzone() when time zone name is unavailable.
Install the package using pip: `pip install tzlocal`
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).
Use `datetime.datetime.replace()` or `datetime.datetime.astimezone()` to make a naive datetime object timezone-aware: ```python 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) ```
Import the function directly from the `tzlocal` module: ```python from tzlocal import get_localzone ```