2

I'm having a problem when building an exe from my scripts that use the pytz lib. I'm constantly getting the error:

File "pytz\__init__.pyc", line 180, in timezone UnknownTimeZoneError: 'Europe\Ljubljana'

No matter how I build the exe I get this error. Running the script works. I tried all suggestions that were posted here and on other sites.

My setup.py file:

from distutils.core import setup
import os.path
try:
    import py2exe
    has_py2exe = True
except ImportError, e:
    has_py2exe = False

myScript=__import__("myScript")

options = {'py2exe': {'packages': ['pytz']}}

options['py2exe'] = {'dist_dir': 'dist'}
setup(
    name="myScript",
    version=myScript.CONST_VERSION,
    console=[{
        'script': 'myScript.py',
        'copyright': 'None',
        'company_name': 'None'
    }],
    options=options,
)

if has_py2exe:
    import zipfile
    zipfile_path = os.path.join(options['py2exe']['dist_dir'], 'library.zip')
    z = zipfile.ZipFile(zipfile_path, 'a')

    import pytz
    assert (pytz.__file__.endswith('__init__.pyc') or pytz.__file__.endswith('__init__.py')), pytz.__file__
    zoneinfo_dir = os.path.join(os.path.dirname(pytz.__file__), 'zoneinfo')
    disk_basedir = os.path.dirname(os.path.dirname(pytz.__file__))
    for absdir, directories, filenames in os.walk(zoneinfo_dir):
        assert absdir.startswith(disk_basedir), (absdir, disk_basedir)
        zip_dir = absdir[len(disk_basedir):]
        for f in filenames:
            z.write(os.path.join(absdir, f), os.path.join(zip_dir, f))
    z.close()

This builds the exe and includes the zoneinfo dir and all timezone files to library.zip.

I only use the pytz in one function where I convert the CEST timestamp to UTC timestamp to store on the server.

The function:

from datetime import datetime
import pytz

def date_time_utc(date_str):
    date_tz = pytz.timezone("Europe/Ljubljana")

    fmt = '%Y-%m-%d %H:%M:%S'
    date_str_dt_object = datetime.strptime(date_str, fmt)

    date_str_dt_object = date_tz.localize(date_str_dt_object)
    date_utc = date_str_dt_object.astimezone(pytz.timezone('UTC'))

    return date_utc.strftime(fmt)

The setup script for the pytz is from here. All other things like this script and any other suggested fixes raised the exactly same error. Has any one figured out how to get this working?

Sam_slo
  • 141
  • 1
  • 1
  • 10
  • I worked around this problem by formatting the date with query because MSSQL has this option "SELECT *, DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), myDate) AS utc_myDate". But I would really like to know how to build a exe with working pytz lib – Sam_slo Mar 23 '18 at 14:36

0 Answers0