gpt4 book ai didi

python - pytz 中缺少时区信息?

转载 作者:太空狗 更新时间:2023-10-29 21:20:59 31 4
gpt4 key购买 nike

我在使用 Python 的 pytz 时遇到了一个非常奇怪的问题:它似乎在我的系统(MacOS X 10.8.5,系统 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'

因此找不到时区'US/Eastern'。因此,我试图查看 pytz 提供的时区目录:

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

正如您所见,它过早地结束了,并且缺少许多时区。我试图通过更新内部使用的 Olson 数据库 pytz 来克服这个问题:

$ sudo pip install -U pytz

但是问题依旧...

知道这里可能出了什么问题吗?我错过了什么吗?

最佳答案

当我尝试在带有最新 ubuntu 图像的 docker 容器中运行我的应用程序时,我遇到了类似的异常 UnknownTimeZoneError: Can not find any timezone configuration。结果发现 tzdata 不见了。安装 tzdata 包修复了它:

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

(必要时使用 sudo)

关于python - pytz 中缺少时区信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21717411/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com