简体   繁体   中英

Python: Return list of public holidays for a specific country

I am trying a get a list public holidays for a country using the Python Holidays package. I have tried following the steps on the Git Repo and a number of other tutorial websites but I keep getting:

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

I installed the package pip install holidays

Then followed this example from geeksforgeeks :

from datetime import date 
import holidays 

uk_holidays = holidays.UnitedKingdom() 

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

I have also tried changing the country to another one specified in the documentation, but It still throws the same AttributeError.

I fairly new to working with python so I am not really sure what I am doing wrong. Any help is greatly appreciated. Thank you

Maybe try installing via anaconda? https://docs.conda.io/en/latest/miniconda.html

First create a conda environment (named py37 below, name it how you wish), then perform the installation within the environment and execute the script. There may be some weird thing in your base python installation causing the library to not work.

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

After this, run the code just as below. If you're using a text editor, you'll need to open that editor from the environment. Or just run from a python kernel through the command line.

我机器上的代码

I think you should check if the latest version of pip is install on your machine using pip -v if not installed, install it and reinstall the holidays module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM