10

I've noticed that pytz misses zoneinfo folder when I try to roll a zip for Windows. Right now I have a workaround that I use after python setup.py build, namely

7z a -xr!*.py* build\exe.win32-2.7\library.zip C:\Python27\Lib\site-packages\pytz

Is there a proper way to achieve that from setup.py or something?

mlt
  • 1,595
  • 2
  • 21
  • 52
  • You can try the zip-includes option. Docs: http://cx_freeze.readthedocs.org/en/latest/distutils.html – Thomas K May 17 '12 at 12:28
  • This option is for adding files into final ZIP if I build it. In my case I'm talking about library.zip and not the final ZIP for redistribution. I'm getting `error: error in setup script: command 'build_exe' has no such option 'zip-includes'` if I try what you suggest. Usually one uses _packages_ option to include code in there, but _pytz_ has just data files and this option fails if I try to add a "package" _pytz/zoneinfo_. – mlt May 18 '12 at 22:32
  • 1
    Try giving it as `zip_includes`, distutils can be funny about dashes/underscores. I think 'zip-includes' is talking about library.zip - cx_Freeze doesn't automatically zip everything up for distribution, although of course you can do that yourself. – Thomas K May 18 '12 at 22:50
  • Huh! Indeed it is funny about underscore. Is it mentioned anywhere? However it silently do nothing. It does create a folder in the destination library.zip but it is empty. Here is what I supply among other stuff in build_exe option `"zip_includes": [("C:/Python27/Lib/site-packages/pytz/zoneinfo/", "pytz/zoneinfo/")]` . pytz/zoneinfo/ is empty:( Also `python setup.py bdist` does create ZIP in dist folder. – mlt May 21 '12 at 06:08
  • I don't think it's mentioned anywhere in cx_Freeze's docs. If I can work it out, I'll add a note. Maybe it doesn't automatically copy whole folders? You might have to use `os.listdir()` to build a list of the files you want. – Thomas K May 21 '12 at 11:36

2 Answers2

12

You could fix this, adding the following method:

def include_files():
        path_base = "C:\\Python27\\Lib\\site-packages\\pytz\\zoneinfo\\"
        skip_count = len(path_base) 
        zip_includes = [(path_base, "pytz/zoneinfo/")]
        for root, sub_folders, files in os.walk(path_base):
            for file_in_root in files:
                zip_includes.append(
                        ("{}".format(os.path.join(root, file_in_root)),
                         "{}".format(os.path.join("pytz/zoneinfo", root[skip_count:], file_in_root))
                        ) 
                )      
        return zip_includes

Then, into setup.py file:

build_exe_options = {"packages": ["os"],
                     "excludes": ["tkinter"],
                     "zip_includes": include_files(),
                     ...
                     }

Hope that helps

matuuar
  • 399
  • 4
  • 9
  • 1
    This worked great for me, thanks! I had to make one minor tweak: skip_count needed 1 added to it otherwise it had a leading slash, which blows away the first argument to os.path.join. – Motoma Mar 13 '15 at 13:42
  • hm, python 3.4. Zone info is included in `pytz\zoneinfo` but not found – Winand Mar 15 '16 at 12:14
2

I've solved this problem in Python 3.4 in the following way

import pytz
setup(
    ...
    options = {'build_exe':
        {'include_files': (pytz.__path__[0],), ...},
    }, 
)

Then pytz is included unzipped with all its time zones

Winand
  • 2,093
  • 3
  • 28
  • 48