Question

I'm having a very weird problem with Python's pytz: it seems to have an incomplete catalog of timezones on my system (MacOS X 10.8.5, system Python 2.7.5).

>>> from pytz import timezone
>>> import pytz

>>> utc = pytz.utc
>>> utc.zone
'UTC'

>>> eastern = timezone('US/Eastern')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pytz/__init__.pyc", line 182, in timezone

pytz.exceptions.UnknownTimeZoneError: 'US/Eastern'

So the timezone 'US/Eastern' can't be found. Accordingly I tried to have a look at the catalog of timezones that pytz offers:

>>> from pytz import all_timezones
>>> for tz in pytz.all_timezones:
...     print tz
... 
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
Africa/Cairo
Africa/Casablanca
Africa/Ceuta
Africa/Conakry
Africa/Dakar
Africa/Dar_es_Salaam
Africa/Djibouti
Africa/Douala
Africa/El_Aaiun
Africa/Freetown
Africa/Gaborone
Africa/Harare
Africa/Johannesburg
Africa/Juba
Africa/Kampala
Africa/Khartoum
Africa/Kigali
Africa/Kinshasa
Africa/Lagos
Africa/Libreville
Africa/Lome
Africa/Luanda
Africa/Lubumbashi
Africa/Lusaka
Africa/Malabo
Africa/Maputo
Africa/Maseru
Africa/Mbabane
Africa/Mogadishu
Africa/Monrovia
Africa/Nairobi
Africa/Ndjamena
Africa/Niamey
Africa/Nouakchott
Africa/Ouagadougou
Africa/Porto-Novo
Africa/Sao_Tome
Africa/Timbuktu
Africa/Tripoli
Africa/Tunis
Africa/Windhoek
America/Adak
America/Anchorage
America/Anguilla
America/Antigua
America/Araguaina
America/Argentina/Buenos_Aires
America/Argentina/Catamarca
America/Argentina/ComodRivadavia
America/Argentina/Cordoba
America/Argentina/Jujuy
America/Argentina/La_Rioja
America/Argentina/Mendoza
America/Argentina/Rio_Gallegos
America/Argentina/Salta
America/Argentina/San_Juan
America/Argentina/San_Luis
America/Argentina/Tucuman
America/Argentina/Ushuaia
America/Aruba
America/Asuncion
America/Atikokan
America/Atka
America/Bahia
America/Bahia_Banderas
America/Barbados
America/Belem
America/Belize
America/Blanc-Sablon
America/Boa_Vista
America/Bogota
America/Boise
America/Buenos_Aires
America/Cambridge_Bay
America/Campo_Grande
America/Cancun
America/Caracas
America/Catamarca
America/Cayenne
America/Cayman
America/Chicago
America/Chihuahua
America/Coral_Harbour
America/Cordoba
America/Costa_Rica
America/Creston
America/Cuiaba
America/Curacao
America/Danmarkshavn
America/Dawson
America/Dawson_Creek
America/Denver
America/Detroit
America/Dominica
America/Edmonton
America/Eirunepe
America/El_Salvador
America/Ensenada
America/Fort_Wayne
America/Fortaleza
America/Glace_Bay
America/Godthab
America/Goose_Bay
America/Grand_Turk
America/Grenada
America/Guadeloupe
America/Guatemala
America/Guayaquil
America/Guyana
America/Halifax
America/Havana
America/Hermosillo
America/Indiana/Indianapolis
America/Indiana/Knox
America/Indiana/Marengo
America/Indiana/Petersburg
America/Indiana/Tell_City
America/Indiana/Vevay
America/Indiana/Vincennes
America/Indiana/Winamac
America/Indianapolis
America/Inuvik
America/Iqaluit
America/Jamaica
America/Jujuy
America/Juneau
America/Kentucky/Louisville
America/Kentucky/Monticello
America/Knox_IN
America/Kralendijk
America/La_Paz
America/Lima
America/Los_Angeles

So as you can see, it ends rather prematurely and many many timezones are missing. I tried to overcome this by updating the Olson database pytz uses internally:

$ sudo pip install -U pytz

But the problem persists...

Any ideas what might be going wrong here? Am I missing something?

Was it helpful?

Solution 3

I cannot say why your installation of pytz is broken, but here's a possible fix:

  1. Download the .zip archive of pytz from the Python Package Index.
  2. In Terminal.app, run pip show pytz.
  3. Using the path it returns, run open /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (replacing my path with yours if different). This will launch a Finder window with your Python modules.
  4. Find the pytz/ folder. Open it.
  5. Replace the zoneinfo/ folder with the zoneinfo/ folder that's in the .zip archive you downloaded in step 1 from PyPI.

OTHER TIPS

I had a similar exception UnknownTimeZoneError: Can not find any timezone configuration when i tried to run my app inside a docker container with the latest ubuntu images. It turned out that tzdata was missing. Installing tzdata package fixed it:

apt-get install -y tzdata

# Maybe you will need to reconfigure the timezone as well:
ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime
dpkg-reconfigure -f noninteractive tzdata

(Use sudo if you have to)

Understand that the time zone data in pytz comes from the IANA time zone database, also known as the Olson database, or simply the tz database.

In this data, certain identifiers (such as US/Eastern) are simply pointers (aka "links" or "aliases") to the real time zone. Links are there for several different reasons, usually for backwards compatibility purposes. In this case, the US/Eastern time zone is a link to America/New_York, which is the true time zone that you should be using. (I believe this particular switch happened in 1993).

You can see other time zones that are just there for backwards compatibility here. See also this chart on Wikipedia which lists the time zones, and clearly indicates which zones are links and where those links point to.

As to why pytz isn't accepting backward compatible zones on your system, I'm not exactly certain. It certainly should, and even shows these in their documentation. You might try re-installing it as Jacob suggested. But even then, you should prefer America/New_York instead of US/Eastern.

Tried reinstall, but bug not gone.

Then, I open pytz/__init__.py, add a line zone = 'UTC', problem is gone:

zone = _unmunge_zone(zone)
zone = 'UTC'
if zone not in _tzinfo_cache:
    if zone in all_timezones_set:
        fp = open_resource(zone)
        try:
            _tzinfo_cache[zone] = build_tzinfo(zone, fp)
        finally:
            fp.close()
    else:
        raise UnknownTimeZoneError(zone)

This is quick and simple solution, but you'd better find the real problem( version? system? ) if you have time.

All solutions didn't work for me in a Spark cluster. But I found the "pendulum" library and it works just fine.

import pendulum
timezone = pendulum.timezone('Europe/Saratov')

For me it as was easy as updating the pytz package via pip3 -install pytz --update On AWS linux machines I had to execute it like this python3 -m pip install pytz --update

My app was complaining about unknown timezone 'America/Punta_Arenas'

I ran into a similar issue and the problem ended up being that the pip installation had previously failed. This fixed it for me:

pip uninstall -y pytz
pip install pytz

I had a similar problem (Mac and python 3.6). it found many timezones but none under America. For some reason (may due to pycharm settings), there was a case conflict in directory names under directory /lib/python3.6/site-packages/pytz/zoneinfo For example, there was an empty america directory and a directory called america (Case Conflict) (and other similar). Renaming this directory to America solved the problem.

Look into pytz/init.py and see if your zone exists. In my case 'America/Argentina/Buenos_Aires'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top