Python:Return 特定国家/地区的 public 假期列表

Python: Return list of public holidays for a specific country

我正在尝试使用 Python Holidays package. I have tried following the steps on the Git Repo 和许多其他教程网站获取某个国家/地区的 public 假期列表,但我不断获取:

AttributeError: module 'holidays' has no attribute 'UnitedKingdom'

我安装了包 pip install holidays

然后按照 geeksforgeeks 中的这个例子:

from datetime import date 
import holidays 

uk_holidays = holidays.UnitedKingdom() 

for ptr in holidays.UnitedKingdom(years = 2018).items(): 
 print(ptr)

我也试过将国家更改为文档中指定的另一个国家,但它仍然抛出相同的 AttributeError。

我对使用 python 还很陌生,所以我不确定自己做错了什么。任何帮助是极大的赞赏。 谢谢

也许尝试通过 anaconda 安装? https://docs.conda.io/en/latest/miniconda.html

首先创建一个conda环境(下面命名为py37,随意命名),然后在环境中进行安装,执行脚本。您的基础 python 安装中可能有一些奇怪的东西导致库无法工作。

conda create --name py37 python=3.7 
conda activate py37
conda install -c conda-forge holidays

此后,运行代码如下。如果您使用的是文本编辑器,则需要从环境中打开该编辑器。或者只是 运行 从 python 内核通过命令行。

我认为你应该检查你的机器上是否安装了最新版本的 pip pip -v 如果没有安装,请安装它并重新安装假期模块