sentinelhub-py icon indicating copy to clipboard operation
sentinelhub-py copied to clipboard

PyInstaller Support

Open wscullen opened this issue 6 years ago • 2 comments

I'm having difficulty with using sentinelhub in a python app frozen with PyInstaller. Right now I am only using AwsProductRequest, so there might be more issues, but the main problem is that constants.py uses a file reference to load the version number from the init file. When frozen to a single .exe, that file reference doesn't work any more.

A possible change would make this work: in constants.py

from . import __version__

mimetypes.add_type('application/json', '.json')

class PackageProps:
    """ Class for obtaining package properties. Currently it supports obtaining package version."""

    @staticmethod
    def get_version():
        version = __version__

and in __init__.py

"""
This module lists all externally useful classes and functions
"""
__version__ = "2.4.1"

from .data_request import WmsRequest, WcsRequest, AwsTileRequest, AwsProductRequest, GeopediaWmsRequest, \
    get_safe_format, download_safe_format

So you move the __version__ initialization to the top of __init__.py and then import the constant directly using from . import __version__ in the constants.py file.

This would allow PyInstaller to bundle sentinelhub successfully. Hope you can consider this change. Thanks.

wscullen avatar Aug 24 '18 21:08 wscullen

Hi @sscullen,

I agree, we can try to make the package work with PyInstaller.

In the newly released package version 2.4.2 we implemented your proposed change. Class sentinelhub.constants.PackageProps now imports the package version instead of reading and parsing the file. The only difference from your proposal is that we moved package version from __init__.py into separate module _version.py in order to avoid any possible cyclic imports in the future.

I haven't tested yet if now PyInstaller can compile the package, however there might still be a problem when package will try to read user-defined settings from config.json into sentinelhub.config.SHConfig class.

Thanks for raising this issue and we will be happy to add any more improvements about this.

AleksMat avatar Aug 28 '18 07:08 AleksMat

Thanks a lot! I've been going through some of my other code and it was common I was taking for granted where my module was running by using 'os.getcwd()' or something else. For the onefile PyInstaller you can check if running in a "frozen" state, which for windows means it is running from a temp folder in ~/AppData/Local/Temp/randomGenName

You can check for being frozen:

# PyInstaller considerations
if getattr(sys, 'frozen', False):
        # we are running in a bundle
        bundle_dir = sys._MEIPASS
else:
        # we are running in a normal Python environment
        bundle_dir = os.path.dirname(os.path.abspath(__file__))

One other thing I noticed when running Sentinel-Hub while frozen was a message about tifffile.py being missing:

C:\Users\cullens\AppData\Local\Temp\_MEI110562\tifffile.py:8209: UserWarning: No module named '_tifffile'
  Functionality might be degraded or be slow.

I'm still new to PyInstaller so I'm not sure of the solution, but I will post here if I come across it.

Thanks again!

wscullen avatar Aug 29 '18 13:08 wscullen

Closing due to long-term inactivity

zigaLuksic avatar Jan 18 '23 09:01 zigaLuksic