Python Py2exe Error After Compile

315 views Asked by At

I have a pretty complicated application I am writing that integrates a lot of modules. Matplotlib was giving some errors until I explicitly included it in the py2exe file. I am getting this error after I compile and try to launch the program and I don't understand what it means. Any help appreciated!

Py2exe compilation file:

from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')

setup(
    options = {
        'py2exe': {
            'optimize': 2,
            'includes' : ["matplotlib.backends.backend_tkagg"],
            'packages' :  ['matplotlib', 'pytz'],
        }
    },
    windows = [{'script': "MYAPP.py", "icon_resources": [(1, "s.ico")]}],
    zipfile = "shared.lib",

Error when launching after compile:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\pint\unit.py", line 756, in load_definitions
    with closing(pkg_resources.resource_stream(__name__, file)) as fp:
  File "C:\Python34\lib\site-packages\pkg_resources\__init__.py", line 1167, in resource_stream
    self, resource_name
  File "C:\Python34\lib\site-packages\pkg_resources\__init__.py", line 1602, in get_resource_stream
    return io.BytesIO(self.get_resource_string(manager, resource_name))
  File "C:\Python34\lib\site-packages\pkg_resources\__init__.py", line 1605, in get_resource_string
    return self._get(self._fn(self.module_path, resource_name))
  File "C:\Python34\lib\site-packages\pkg_resources\__init__.py", line 1683, in _get
    return self.loader.get_data(path)
OSError: [Errno 0] Error: 'pint\\default_en.txt'
1

There are 1 answers

0
Paul B On

I think the problem here lies with py2exe's dealing with txt files. It looks like you're using the pint module and within pint is the file default_en.txt (which your compiled app is complaining about). When py2exe compiles pint it ignores the txt files contained within the pint package.

The workaround I used was within my script to state:

ureg = pint.UnitRegistry('\path\to\default_en.txt')

also, I added the following to my setup.py file for py2exe

"packages":["pkg_resources"],

from: http://pint.readthedocs.org/en/0.6/index.html

If you make a translation of the default units or define a completely new set, you don’t want to append the translated definitions so you just give the filename to the constructor:

from pint import UnitRegistry ureg = UnitRegistry('/your/path/to/default_es.txt')

I made \path\to\ a location within my project and added default_en.txt and constants_en.txt to that folder (copied from pint folder). Then in py2exe I added these files to my data_files.

The real solution would be to figure out how to get py2exe include the txt files in module folders but I haven't yet figured that out.