Timezone information Missing in pytz?

I have a very strange problem with Python pytz : I have an incomplete timezone directory on my system (MacOS X 10.8.5, Python 2.7.5 system).

 >>> 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' 

Therefore, the time zone 'US/Eastern' cannot be found. Accordingly, I tried to take a look at the timezone directory 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, this ends prematurely, and many many time zones are missing. I tried to overcome this by updating the Olson pytz database using internally:

 $ sudo pip install -U pytz 

But the problem persists ...

Any ideas what might be wrong here? Did I miss something?

+6
source share
7 answers

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

  • Download the .zip pytz archive from the Python Package Index .
  • In Terminal.app, run pip show pytz .
  • Using the return path, run open /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (replacing my path with yours if it is different). This will launch the Finder window with your Python modules.
  • Find the pytz / folder. Open it.
  • Replace the infoinfo / folder with the infoinfo / folder, which is in the .zip archive that you downloaded in step 1 of PyPI.
+3
source

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

In this data, certain identifiers (for example, US/Eastern ) are simply pointers (so-called "links" or "aliases") to the real-time zone. Links exist for several reasons, usually for backward compatibility purposes. In this case, the US/Eastern time zone is a reference to America/New_York , which is the true time zone that you should use. (I believe that this particular transition occurred in 1993).

You can see other time zones that simply exist for backward compatibility here . See also this table on Wikipedia, which lists time zones and clearly indicates which zones are links and which these links point to.

As for why pytz doesn't accept backward compatible zones on your system, I'm not quite sure. It certainly follows, and even shows it in its documentation. You can try reinstalling it, as Jacob suggested. But even then you should prefer America/New_York instead of US/Eastern .

+6
source

I had a similar exception. UnknownTimeZoneError: Can not find any timezone configuration when I tried to run my application in a docker container with the latest Ubuntu images. It turned out that tzdata was missing. Installing the tzdata package fixed this:

 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 necessary)

+5
source

I tried to reinstall, but the error did not disappear.

Then I open pytz/__init__.py , add the line zone = 'UTC' , the problem disappeared:

 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 a quick and easy solution, but you better find the real problem (version? System?) If you have the time.

+1
source

For me, it was as simple as updating a pytz package through pip3 -install pytz --update On Linux AWS machines, I had to run it like this python3 -m pip install pytz --update

My application complained about unknown timezone 'America/Punta_Arenas'

0
source

I ran into a similar problem and the problem ended up installing pip previously unsuccessful. This fixed it for me:

 pip uninstall -y pytz pip install pytz 
0
source

All solutions did not help me in the Spark cluster. But I found the pendulum library, and it works just fine.

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

All Articles