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

using pylibjpeg with executable creator libraries #55

Open
m-aminiz opened this issue Oct 14, 2020 · 7 comments
Open

using pylibjpeg with executable creator libraries #55

m-aminiz opened this issue Oct 14, 2020 · 7 comments
Labels
enhancement New feature or request

Comments

@m-aminiz
Copy link

Hi. thank you for your great library. I write a code that uses pylibjpeg. the code works pretty well. I created an executable file of my code but in the generated .exe program, pylibjpeg doesn't work and it gives me GDCM handler error. I think the problem happened because of pylibjpeg-libjpeg. for creating .exe with cx_freeze I should determine the package that used in my code in a list. when I add pylibjpeg-libjpeg to that list .exe creator code gives me error(No module named 'pylibjpeg-libjpeg'). I tried other .exe creator library like pyinstaller but the problem was same. They did not recognize pylibjpeg-libjpeg and did not add it to the .exe lib folder

@scaramallion
Copy link
Member

Thanks, I'll take a look to see if I can figure out what's going on.

@pouryashirazian
Copy link

I also second this issue. @scaramallion it would be great if you could provide hooks for pyinstaller as mentioned in their doc here.

@scaramallion scaramallion added the enhancement New feature or request label Apr 11, 2021
@shensmobile
Copy link

shensmobile commented Jul 15, 2021

Late addition to the discussion, but have been trying to fight through packing up a PyDicom script using PyInstaller and running into the same issue. @m-aminiz's issue is likely linked to the fact that the libjpeg plugin's library package name isn't "pylibjpeg-libjpeg" but rather, it is "libjpeg". This goes for the openjpeg and rle plugins as well.

I have been running into another issue PyInstaller and pylibjpeg. pylibjpeg.utils uses pkg_resources, specifically the iter_entry_points method. PyInstaller doesn't work with pkg_resources, even if you import it as a hidden import. See here for more:

pyinstaller/pyinstaller#3050

I have no idea what this fellow is doing and how to implement it myself so I think I'm kind of stuck unfortunately :(

@mshunshin
Copy link

Just for reference for those that are stuck - here is what worked for me from the above links.

Firstly make the spec file as usual - you do not need to use any of the command line options to get it to include pylibjpeg

pyinstaller my_script.py

or if you are making an .app file:

pdm run pyinstaller my_script.py --onefile --windowed --osx-bundle-identifier com.my_company.my_script

Then, to the my_script.spec file add this to the top before the line that starts a = Analysis(['my_script'.py],

# -*- mode: python ; coding: utf-8 -*-

import pkg_resources
import os

hook_ep_packages = dict()
hiddenimports = set()

# List of packages that should have there Distutils entrypoints included.
ep_packages = [
    "pylibjpeg.jpeg_decoders",
    "pylibjpeg.jpeg_xt_decoders",
    "pylibjpeg.jpeg_ls_decoders",
    "pylibjpeg.jpeg_2000_decoders",
    "pylibjpeg.jpeg_xr_decoders",
    "pylibjpeg.jpeg_xs_decoders",
    "pylibjpeg.jpeg_xl_decoders",
    "pylibjpeg.pixel_data_decoders"]

if ep_packages:
    for ep_package in ep_packages:
        for ep in pkg_resources.iter_entry_points(ep_package):
            if ep_package in hook_ep_packages:
                package_entry_point = hook_ep_packages[ep_package]
            else:
                package_entry_point = []
                hook_ep_packages[ep_package] = package_entry_point
            package_entry_point.append("{} = {}:{}".format(ep.name, ep.module_name, ep.attrs[0]))
            hiddenimports.add(ep.module_name)

    try:
        os.mkdir('./generated')
    except FileExistsError:
        pass

    with open("./generated/pkg_resources_hook.py", "w") as f:
        f.write("""# Runtime hook generated from spec file to support pkg_resources entrypoints.
ep_packages = {}

if ep_packages:
    import pkg_resources
    default_iter_entry_points = pkg_resources.iter_entry_points

    def hook_iter_entry_points(group, name=None):
        if group in ep_packages and ep_packages[group]:
            eps = ep_packages[group]
            for ep in eps:
                parsedEp = pkg_resources.EntryPoint.parse(ep)
                parsedEp.dist = pkg_resources.Distribution()
                yield parsedEp
        else:
            return default_iter_entry_points(group, name)

    pkg_resources.iter_entry_points = hook_iter_entry_points
""".format(hook_ep_packages))



block_cipher = None

Then to the hidden imports line change it to: hiddenimports=list(hiddenimports),

and the runtime hooks line to: runtime_hooks=["./generated/pkg_resources_hook.py"],

then re-run pyinstaller with

pyinstaller my_script.spec

@csheaff
Copy link

csheaff commented Feb 17, 2023

Hi @mshunshin , thanks very much for the solution above. As it is not clear to me that repo licenses extent to issue comments, can you confirm your intention for the above code to be included in the repository or in another with similar licensing (MIT)?

@mshunshin
Copy link

I can confirm that any code of which I am an author, contributed within any of my submitted code or comments, and to the pydicom project can used under any licence compatible with the pydicom project licensing, specifically including the MIT license.

@csheaff
Copy link

csheaff commented Feb 17, 2023

Thanks @mshunshin !

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

No branches or pull requests

6 participants