Search code examples
pythonexepy2exedistutils

RuntimeError when using py2exe to convert program and passing options


I'm attempting to convert a python program into an executable using py2exe, following the tutorial flow. I've created a setup.py as below (using this example).

    from distutils.core import setup
    import py2exe

    options = {
        'py2exe': {
            'compressed': 1,
            'optimize': 2,
            'bundle_files': 3, #Options 1 & 2 do not work on a 64bit system
            'dist_dir': 'dist',  # Put .exe in dist/
            'xref': False,
            'skip_archive': False,
            'ascii': False}}

    setup(options=options, zipfile=None, console=['main.py'])

When I run python setup.py py2exe in Anaconda command prompt (set up to run a python 3.5 environment), I get the following error trace:

    running py2exe
    Traceback (most recent call last):
      File "setup.py", line 14, in <module>
        }, zipfile=None, console=['main.py'])
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\distutils\core.py", line 148, in setup
        dist.run_commands()
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\distutils\dist.py", line 955, in run_commands
        self.run_command(cmd)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\distutils\dist.py", line 974, in run_command
        cmd_obj.run()
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run
        self._run()
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run
        builder.analyze()
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\runtime.py", line 173, in analyze
        target.analyze(mf)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\runtime.py", line 74, in analyze
        modulefinder.run_script(self.script)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 86, in run_script
        self._scan_code(mod.__code__, mod)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 394, in _scan_code
        self.safe_import_hook(name, mod, fromlist, level)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 138, in safe_import_hook
        self.import_hook(name, caller, fromlist, level)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
        module = self._gcd_import(name)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import
        return self._find_and_load(name)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 357, in _find_and_load
        self._scan_code(module.__code__, module)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 394, in _scan_code
        self.safe_import_hook(name, mod, fromlist, level)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 138, in safe_import_hook
        self.import_hook(name, caller, fromlist, level)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
        module = self._gcd_import(name)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import
        return self._find_and_load(name)
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 356, in _find_and_load
        if module.__code__:
      File "C:\Users\<username>\AppData\Local\conda\conda\envs\py35_env\lib\site-packages\py2exe\mf3.py", line 637, in __code__
        raise RuntimeError("should read __file__ to get the source???")
    RuntimeError: should read __file__ to get the source???

I tried following the traceback, but wasn't able to figure out the root of the error. Any suggestions on what my issue is and how to resolve it?


Solution

  • It appears my error was related to a lack of support in py2exe for python 3.5. I ran into a similar (though not identical error) when I ran the tutorial example (hello world) listed in the tutorial linked in my question, which is how I identified the issue.

    I created a new Anaconda environment with a python 3.4 interpreter, and was able to build an executable for my original application successfully.