2

I am trying to use pytz in a python script, to be used as a mapper for a hadoop streaming job.

Following advice in another thread, I tried packaging pytz as a zip 'pytz.mod', and loading it with zipimport:

import zipimport

importer = zipimport.zipimporter('pytz.mod')
pytz = importer.load_module('pytz')

from pytz import timezone
user_timezone = timezone('America/Moncton')

This gives the following error:

Traceback (most recent call last):
  File "./load-pytz.py", line 15, in <module>
    user_timezone = timezone('America/Moncton')
  File "./lib/python2.6/site-packages/pytz/__init__.py", line 180, in timezone
pytz.exceptions.UnknownTimeZoneError: 'America/Moncton'

I found this thread, and the issue seems to be that zipimport can't load binary files in the zoneinfo directory. I tried to follow instructions in the thread but could not get them to work. Is there a simpler mechanism for packaging zoneinfo, without going through py2exe?

All I need to do is to localize a UTC time stamp relative to a time zone, e.g., 'America/Moncton'.

Thank you!

Community
  • 1
  • 1

1 Answers1

2

Have you tried tzlocal? From what i read Here, pytz will give an unknown timezone error for a a few reasons. - If you dont give it a timezone name, or if the timezone name you give it, depending on your operating system, isn't be the same as the timezone names pytz uses. Using tzlocal to retrieve your local timezone as a tzinfo object, to use with pytz as normal, should let you continue with your project.

As shown in the link i've placed above, you can do this by

from tzlocal import get_localzone
 tz = get_localzone()

I dont claim to be an expert, just doing some research of my own to try to help out a fellow in need. Hope this helps you.

Colabambino
  • 504
  • 1
  • 4
  • 11
  • tzlocal is great, but I'm not sure what it has to do with the question asked. – Matt Johnson-Pint Apr 23 '15 at 23:55
  • My understanding was that the op couldnt get a timezone that pytz would recognize and was thus causing him issues, pardon if im incorrect, i assumed that the error was within their not recieving a local timezone . – Colabambino Apr 24 '15 at 01:50
  • I should have checked pytz's timezone names before posting, however i suppose now tzlocal has been spread for anyone it may help. My apologies on the misguided answer, to all. – Colabambino Apr 24 '15 at 12:54
  • Thanks for responding! I do already know the time zone, but it's true that I don't really need to use pytz. I could simply use the python-dateutil library, following this post: http://stackoverflow.com/questions/4770297/python-convert-utc-datetime-string-to-local-datetime – Alina Beygelzimer Apr 24 '15 at 14:53