astropy: Error building executable using pyinstaller

Building executables which include astropy seem to have a history of issues (e.g. https://github.com/astropy/astropy/pull/960#issuecomment-355678708). Initially I was getting an, ImportError: Astropy requires the 'six' module of minimum version 1.10, which I solved by using a trick given in the above PR:

I was able to get this to work using PyInstaller with the following pyinstaller options --exclude-module astropy and --add-data path_to/astropy:astropy@fridgerator

Now I get the new error:

Traceback (most recent call last):
  File "pyapi.py", line 10, in <module>
    import cosmopy
  File "/Users/lzkelley/Programs/cosmo/ve_test/vetest/lib/python3.5/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages/cosmopy/__init__.py", line 9, in <module>
  File "/var/folders/wr/_dhmn18x1dj6t8ytjjd9rsxh0000gn/T/_MEIhGawWS/astropy/constants/__init__.py", line 28, in <module>
    from .constant import Constant, EMConstant
  File "/var/folders/wr/_dhmn18x1dj6t8ytjjd9rsxh0000gn/T/_MEIhGawWS/astropy/constants/constant.py", line 11, in <module>
    from ..units.core import Unit, UnitsError
  File "/var/folders/wr/_dhmn18x1dj6t8ytjjd9rsxh0000gn/T/_MEIhGawWS/astropy/units/__init__.py", line 14, in <module>
    from .core import *
  File "/var/folders/wr/_dhmn18x1dj6t8ytjjd9rsxh0000gn/T/_MEIhGawWS/astropy/units/core.py", line 23, in <module>
    from .utils import (is_effectively_unity, sanitize_scale, validate_power,
  File "/var/folders/wr/_dhmn18x1dj6t8ytjjd9rsxh0000gn/T/_MEIhGawWS/astropy/units/utils.py", line 16, in <module>
    from fractions import Fraction
ImportError: No module named 'fractions'
[2338] Failed to execute script pyapi

I’m using a virtualenv named vetest in the directory ve_test, trying to run with the command: pyinstaller -F pyapi.py --exclude-module astropy --add-data /Users/lzkelley/Programs/cosmo/ve_test/vetest/lib/python3.5/site-packages/astropy:astropy

The full output from pyinstaller is attached.

out.txt


An additional issue is that the resulting executable is >200MB, is that normal? The application I’m trying to package is really just using the astropy.cosmology.LambdaCDM class. Might there be a better way to include that alone?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 24 (15 by maintainers)

Commits related to this issue

Most upvoted comments

yes, works!

I don’t have a solution, but a potential workaround which worked for me was the following: In the *.spec file, add:

import astropy
astropy_path, = astropy.__path__

Then in the Analysis, add (astropy_path, 'astropy') into the datas list and 'astropy' in the excludes list.

Hope this helps.

@lzkelley - the developer version of Astropy now only supports Python 3

I just tried running:

pyinstaller -F app.py 

with the latest astropy, and the six issue is gone but this is still a problem:

Traceback (most recent call last):
  File "app.py", line 2, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/Users/tom/miniconda3/envs/dev/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "astropy/__init__.py", line 122, in <module>
  File "astropy/__init__.py", line 129, in Conf
  File "astropy/config/configuration.py", line 229, in __init__
RuntimeError: Cannot automatically determine get_config module, because it is not called from inside a valid module
[28432] Failed to execute script app
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

(this error was discussed extensively in #960)

But it might work fine if doing: https://github.com/maartenbreddels/frozen_astropy

It’d be nice in the long run to not require that and have it work ‘out of the box’. For that to work, I think someone needs to go through the changes in #960 and apply the ones that are still relevant.

@burhankhanlodhy thanks this worked for me.

@astrofrog Shouldn’t it be possible to add this to the PyInstaller hook? Otherwise this hook would somehow miss its intended purpose.

Thanks @lamourj for the workaround. I had also to add the astropy dependencies as hiddenimports explicitly though:

# Integrate astropy as data directory instead of module:
import astropy
astropy_path, = astropy.__path__
datas=[(astropy_path, 'astropy')]
excludes=['astropy']
hiddenimports=['shelve', 'csv']    # Required by astropy