Last modified: Jun 09, 2025 By Alexander Williams

How to Install pytz in Python - Quick Guide

Working with time zones in Python can be tricky. The pytz library makes it easier. This guide shows how to install and use pytz.

What Is pytz?

pytz is a Python library for working with time zones. It helps handle datetime conversions across different time zones. It's essential for global applications.

pytz provides accurate time zone data. It works well with Python's built-in datetime module. You can convert times between zones easily.

Install pytz Using pip

The easiest way to install pytz is using pip. pip is Python's package manager. Make sure you have Python installed first.

Open your terminal or command prompt. Run this command:


pip install pytz

This will download and install the latest pytz version. If you need a specific version, specify it like this:


pip install pytz==2023.3

Verify pytz Installation

After installation, verify it works. Open a Python shell and try importing pytz:


import pytz
print(pytz.__version__)

You should see the installed version number. If you get an error, check your installation.

Basic pytz Usage

Here's a simple example of pytz in action. We'll convert a datetime to another time zone.


from datetime import datetime
import pytz

# Create a datetime object
local_time = datetime.now()
print("Local time:", local_time)

# Convert to UTC
utc_time = local_time.astimezone(pytz.utc)
print("UTC time:", utc_time)

# Convert to New York time
ny_time = local_time.astimezone(pytz.timezone('America/New_York'))
print("New York time:", ny_time)

This code shows how to work with different time zones. pytz makes time zone conversions simple.

Common pytz Time Zones

pytz includes all major time zones. Here are some common ones:


import pytz

# List some time zones
print("UTC:", pytz.utc)
print("New York:", pytz.timezone('America/New_York'))
print("London:", pytz.timezone('Europe/London'))
print("Tokyo:", pytz.timezone('Asia/Tokyo'))

You can find a full list of time zones in pytz's documentation. Use the exact names for accuracy.

Handling Daylight Saving Time

pytz handles Daylight Saving Time (DST) automatically. This is a key feature. Here's an example:


import pytz
from datetime import datetime

# Create a datetime in summer and winter
summer = datetime(2023, 7, 1, 12, 0, 0)
winter = datetime(2023, 12, 1, 12, 0, 0)

# Convert to London time
london = pytz.timezone('Europe/London')
print("Summer in London:", summer.astimezone(london))
print("Winter in London:", winter.astimezone(london))

The output will show different offsets for DST. pytz adjusts automatically.

pytz With Other Python Libraries

pytz works well with other Python libraries. For example, you can use it with GeoPandas for time-stamped geographic data.

It also pairs nicely with pycountry for location-based applications. The combination is powerful for global apps.

Troubleshooting pytz Installation

If you have installation issues, try these steps:

1. Upgrade pip first: pip install --upgrade pip

2. Check your Python version matches pytz requirements

3. Try installing in a virtual environment

For more complex setups, like Cartopy, you might need additional dependencies.

Conclusion

pytz is a must-have for time zone handling in Python. Installation is simple with pip. The library offers powerful time zone features.

Remember to always use pytz with Python's datetime module. This ensures accurate time conversions. Now you're ready to work with global time zones in your projects.