Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

astropy 5.0 not working with pyinstaller #379

Closed
EfremBraun opened this issue Feb 9, 2022 · 6 comments · Fixed by #381
Closed

astropy 5.0 not working with pyinstaller #379

EfremBraun opened this issue Feb 9, 2022 · 6 comments · Fixed by #381

Comments

@EfremBraun
Copy link
Contributor

Description of the issue

pyinstaller seems to not work with the latest version of astropy (5.0 and above)

Context information (for bug reports)

  • Output of pyinstaller --version: 4.9
  • Version of Python: 3.9.7
  • Platform: macOSS Big Sur 11.6.2
  • How you installed Python: conda
  • Did you also try this on another platform? Does it work there? I did not try this.

A minimal example program which shows the error

app.py:

from PySide6 import QtWidgets
from astropy.time import Time
import sys

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        Time('1999-01-01T00:00:00.123456789')
        self.setWindowTitle("Hello World")
        l = QtWidgets.QLabel("My simple app.")
        l.setMargin(10)
        self.setCentralWidget(l)
        self.show()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    app.exec()

First, install requisites using `pip install pyinstaller=4.9 pyside6'

Then, let's show that this works with the previous version of Astropy:

  1. Install astropy 4.3 using pip install astropy==4.3
  2. Build the package using pyinstaller app.py.
  3. Run the bundled application using ./dist/app/app
  4. Success!

Then, let's show that this fails with the current version of Astropy:

  1. Install latest version of astropy using pip install astropy==5.0.1
  2. Build the package using pyinstaller app.py.
  3. Run the bundled application using ./dist/app/app
  4. Failure. See stacktrace below.

Stacktrace / full error message

Traceback (most recent call last):
  File "astropy/utils/introspection.py", line 157, in minversion
  File "importlib/metadata.py", line 551, in version
  File "importlib/metadata.py", line 524, in distribution
  File "importlib/metadata.py", line 187, in from_name
importlib.metadata.PackageNotFoundError: numpy

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "app.py", line 2, in <module>
    from astropy.time import Time
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
  File "astropy/time/__init__.py", line 19, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
  File "astropy/time/formats.py", line 16, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
  File "astropy/units/__init__.py", line 17, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
  File "astropy/units/quantity.py", line 25, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
  File "astropy/utils/compat/__init__.py", line 19, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
  File "astropy/utils/compat/numpycompat.py", line 14, in <module>
  File "astropy/utils/decorators.py", line 546, in wrapper
  File "astropy/utils/introspection.py", line 164, in minversion
KeyError: 'numpy'
[16774] Failed to execute script 'app' due to unhandled exception!
@rokm
Copy link
Member

rokm commented Feb 9, 2022

Looks like astropy 5.0 requires numpy's metadata (--copy-metadata numpy).

@rokm
Copy link
Member

rokm commented Feb 9, 2022

Transferring this to contrib hooks repository, where the astropy hook is maintained.

@rokm rokm removed the triage label Feb 9, 2022
@rokm rokm transferred this issue from pyinstaller/pyinstaller Feb 9, 2022
@EfremBraun
Copy link
Contributor Author

EfremBraun commented Feb 15, 2022

Just a note:

When I tried to run pyinstaller with a code using astropy 5.0, running it with --copy-metadata numpy changed the error from KeyError: 'numpy' to KeyError: 'astropy'. I needed to add --copy-metadata astropy in addition to get the program to run without errors.

@EfremBraun
Copy link
Contributor Author

So seemingly, adding the lines datas += copy_metadata('numpy') and datas += copy_metadata('astropy') to the astropy hook solve the issue. But I really don't know enough about what's going on behind on the scenes to put in that pull request. I don't know why astropy 5.0 needs those lines, or whether that's an indication that the astropy maintainers should fix something in their package. So hopefully someone who knows pyinstaller and astropy better can review whether this is the right thing to do.

@bwoodsend
Copy link
Member

It needs those because astropy uses importlib.metadata on astropy and numpy (albeit indirectly) to query their versions. Distribution metadata (the stuff shown by pip show numpy) is stored in numpy-1.22.2.dist-info folders which PyInstaller doesn't to collect by default. datas += copy_metadata('numpy') tells PyInstaller to go and find the numpy-1.22.2.dist-info folder and include it so that frozen importlib.metatdata can find it at runtime.

@EfremBraun
Copy link
Contributor Author

Thank you @bwoodsend! With that background, I feel comfortable putting in the pull request. Doing it now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants