Skip to content

Cannot use python esc/pos library #8659

Description

@tmblog

I am trying to build using the python escpos library, my build is fine without it https://github.com/python-escpos/python-escpos
However any reference to escpos library crashes the program, here's the errors:

Traceback (most recent call last):
  File "app.py", line 3, in <module>
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "database.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "helpers.py", line 13, in <module>
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "escpos\printer\__init__.py", line 4, in <module>
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "escpos\printer\cups.py", line 16, in <module>
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "escpos\escpos.py", line 26, in <module>
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "escpos\capabilities.py", line 48, in <module>
FileNotFoundError: [Errno 2] 
No such file or directory: 'C:\\Users\\xxx\\AppData\\Local\\Temp\\_MEI141202\\escpos\\capabilities\\capabilities.json'

I am not sure why it's looking in a folder called capabilities when the capabilities.json is in escpos folder. There is no such folder.
In capabilities.py line 24
there is a ref to the file, ref = importlib_resources.files(__name__) / "capabilities.json" which I thought could be the issue but I am not sure. Any help is appreciated.
Thank you!

Activity

  1. added
    triagePlease triage and relabel this issue
    on Jul 16, 2024
  2. bwoodsend commented on Jul 16, 2024

    @bwoodsend
    Member

    Have you tried adding --collect-data=escpos to your pyinstaller command?

  3. tmblog commented on Jul 16, 2024

    @tmblog
    Author

    Have you tried adding --collect-data=escpos to your pyinstaller command?

    Thank you for the prompt response.
    I was using:
    --add-data "venv/Lib/site-packages/escpos/capabilities.json;escpos" without luck now I tried what you suggested but it's still not finding that file.

  4. rokm commented on Jul 16, 2024

    @rokm
    Member

    What version of PyInstaller and what version of escpos are you using?

  5. tmblog commented on Jul 16, 2024

    @tmblog
    Author

    What version of PyInstaller and what version of escpos are you using?
    Thanks for your reply. Here are the versions.
    Pyinstaller 6.4.0
    python-escpos Version: 3.1

  6. rokm commented on Jul 16, 2024

    @rokm
    Member

    Version: 3.1

    Ah yes, this is python-escpos, not escpos.

  7. tmblog commented on Jul 16, 2024

    @tmblog
    Author

    Version: 3.1

    Ah yes, this is python-escpos, not escpos.
    It's actually the same library https://pypi.org/project/escpos/ github page is https://github.com/python-escpos/python-escpos
    Inside my virtual environment it's in a folder called escpos, so --collect-data=escpos is correct isn't it?

  8. rokm commented on Jul 16, 2024

    @rokm
    Member

    Inside my virtual environment it's in a folder called escpos, so --collect-data=escpos is correct isn't it?

    It is, but it seems there's a problem with path that we return for importlib_resources.files(__name__) in frozen application.

  9. rokm commented on Jul 16, 2024

    @rokm
    Member

    Until I figure out what it is and fix it, you can use this as a work around:

    import sys
    if getattr(sys, 'frozen', False):
        import os
        os.environ['ESCPOS_CAPABILITIES_FILE'] = os.path.join(sys._MEIPASS, 'escpos', "capabilities.json")
    
    import escpos.printer
  10. rokm commented on Jul 16, 2024

    @rokm
    Member

    Looks like we return submodule path as if it were a package, rather than returning its package path.

    I.e., in unfrozen python, printing importlib_resources.files(__name__) from package / submodule / subpackage / submodule-in-subpackage gives:

    IN PACKAGE: [...]\mypkg
    IN SUBMODULE: [...]\mypkg
    IN SUBPACKAGE: [...]\mypkg\mysubpkg
    IN SUBPACKAGE SUBMODULE: [...]\mypkg\mysubpkg
    

    and in frozen application, it is:

    IN PACKAGE: [...]\program\_internal\mypkg
    IN SUBMODULE: [...]\program\_internal\mypkg\mymod
    IN SUBPACKAGE: [...]\program\_internal\mypkg\mysubpkg
    IN SUBPACKAGE SUBMODULE: [...]\program\_internal\mypkg\mysubpkg\mysubmod
    
  11. added and removed
    triagePlease triage and relabel this issue
    on Jul 16, 2024
  12. tmblog commented on Jul 16, 2024

    @tmblog
    Author

    Until I figure out what it is and fix it, you can use this as a work around:

    import sys
    if getattr(sys, 'frozen', False):
        import os
        os.environ['ESCPOS_CAPABILITIES_FILE'] = os.path.join(sys._MEIPASS, 'escpos', "capabilities.json")
    
    import escpos.printer

    Thank you!
    I take it build using --add-data "venv/Lib/site-packages/escpos/capabilities.json;escpos" and --collect-data escpos

    it worked btw. I'll keep an eye out for future releases.
    Thanks again!

  13. added 2 commits that reference this issue on Jul 17, 2024
    dbc0174
    1492b21
  14. added a commit that references this issue on Jul 18, 2024
    b960fd8
  15. tmblog commented on Jul 22, 2024

    @tmblog
    Author

    Thanks again for this. Can we expect this in the next release?

  16. locked as resolved and limited conversation to collaborators on Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions