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

[QUESTION] Is pyinstaller supported for automatic shell completion? #101

Open
4 tasks done
johnthagen opened this issue May 11, 2020 · 8 comments
Open
4 tasks done
Labels
question Question or problem

Comments

@johnthagen
Copy link

First check

  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already searched in Google "How to X in Click" and didn't find any information.

Description

I am bundling a executable using pyinstaller 3.6 that has a dependency on typer[all].

altgraph==0.17,click==7.1.2,colorama==0.4.3,coloredlogs==14.0,dataclasses==0.6,humanfriendly==8.2,macholib==1.14,PyInstaller==3.6,shellingham==1.3.2,typer==0.2.1

When I run with --show-completion I get the following error:

$ ./my_exe --show-completion
Traceback (most recent call last):
  File "shellingham/__init__.py", line 13, in detect_shell
  File "importlib/__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'shellingham.posix'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "my_exe.py", line 240, in <module>
  File "my_exe.py", line 236, in main
  File "typer/main.py", line 213, in __call__
  File "click/core.py", line 829, in __call__
  File "click/core.py", line 781, in main
  File "click/core.py", line 700, in make_context
  File "click/core.py", line 1212, in parse_args
  File "click/core.py", line 1048, in parse_args
  File "click/core.py", line 1630, in handle_parse_result
  File "click/core.py", line 123, in invoke_param_callback
  File "typer/main.py", line 788, in wrapper
  File "typer/completion.py", line 59, in show_callback
  File "shellingham/__init__.py", line 16, in detect_shell
RuntimeError: Shell detection not implemented for 'posix'
[16390] Failed to execute script my_exe

I wasn't sure if this was a bug, or something that simply isn't supported.

Additional context

Build command:

$ pyinstaller --onefile my_exe.py --hidden-import=pkg_resources.py2_warn
@johnthagen johnthagen added the question Question or problem label May 11, 2020
@wmmc88
Copy link

wmmc88 commented Jun 26, 2021

@johnthagen Did you mange to get pyinstaller to package a typer application? Curious because I might run into the same situation soon.

@johnthagen
Copy link
Author

@wmmc88 No, I disable shell auto completion on all of my Tyler/Pyinstaller apps.

@JPHutchins
Copy link

I see this as well:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ in detect_shell:12                                                                               │
│                                                                                                  │
│ in import_module:126                                                                             │
│ in _gcd_import:1050                                                                              │
│ ╭─────────── locals ────────────╮                                                                │
│ │   level = 1                   │                                                                │
│ │    name = 'shellingham.posix' │                                                                │
│ │ package = 'shellingham'       │                                                                │
│ ╰───────────────────────────────╯                                                                │
│ in _find_and_load:1027                                                                           │
│ ╭────────────────────── locals ──────────────────────╮                                           │
│ │ import_ = <function _gcd_import at 0x7f816973f400> │                                           │
│ │  module = <object object at 0x7f8169764060>        │                                           │
│ │    name = 'shellingham.posix'                      │                                           │
│ ╰────────────────────────────────────────────────────╯                                           │
│ in _find_and_load_unlocked:1004                                                                  │
│ ╭──────────────────────────────────────── locals ────────────────────────────────────────╮       │
│ │       import_ = <function _gcd_import at 0x7f816973f400>                               │       │
│ │          name = 'shellingham.posix'                                                    │       │
│ │        parent = 'shellingham'                                                          │       │
│ │ parent_module = <module 'shellingham' from '/tmp/_MEI9vYRhL/shellingham/__init__.pyc'> │       │
│ │          path = ['/tmp/_MEI9vYRhL/shellingham']                                        │       │
│ │          spec = None                                                                   │       │
│ ╰────────────────────────────────────────────────────────────────────────────────────────╯       │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
ModuleNotFoundError: No module named 'shellingham.posix'

During handling of the above exception, another exception occurred:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /home/jp/repos/smpmgr/smpmgr/__main__.py:5 in <module>                                           │
│                                                                                                  │
│   2                                                                                              │
│   3 from smpmgr.main import app                                                                  │
│   4                                                                                              │
│ ❱ 5 app(prog_name="smpmgr")                                                                      │
│   6                                                                                              │
│                                                                                                  │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │       _pyi_main_co = <code object <module> at 0x7f8168ec82f0, file "smpmgr/__main__.py",     │ │
│ │                      line 1>                                                                 │ │
│ │                app = <typer.main.Typer object at 0x7f8168ec4ca0>                             │ │
│ │          encodings = <module 'encodings' from                                                │ │
│ │                      '/tmp/_MEI9vYRhL/base_library.zip/encodings/__init__.pyc'>              │ │
│ │              entry = '/tmp/_MEI9vYRhL/libbz2.so.1.0'                                         │ │
│ │                 os = <module 'os' from '/tmp/_MEI9vYRhL/base_library.zip/os.pyc'>            │ │
│ │                pth = '/tmp/_MEI9vYRhL'                                                       │ │
│ │ pyimod02_importers = <module 'pyimod02_importers' from                                       │ │
│ │                      '/home/jp/repos/smpmgr/PyInstaller/loader/pyimod02_importers.py'>       │ │
│ │    pyimod03_ctypes = <module 'pyimod03_ctypes' from                                          │ │
│ │                      '/home/jp/repos/smpmgr/PyInstaller/loader/pyimod03_ctypes.py'>          │ │
│ │        python_path = [                                                                       │ │
│ │                      │   '/tmp/_MEI9vYRhL/base_library.zip',                                 │ │
│ │                      │   '/tmp/_MEI9vYRhL/lib-dynload',                                      │ │
│ │                      │   '/tmp/_MEI9vYRhL'                                                   │ │
│ │                      ]                                                                       │ │
│ │                sys = <module 'sys' (built-in)>                                               │ │
│ │            VIRTENV = 'VIRTUAL_ENV'                                                           │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                  │
│ in __call__:328                                                                                  │
│                                                                                                  │
│ in __call__:311                                                                                  │
│                                                                                                  │
│ in __call__:1157                                                                                 │
│                                                                                                  │
│ in main:778                                                                                      │
│                                                                                                  │
│ in _main:215                                                                                     │
│                                                                                                  │
│ in make_context:943                                                                              │
│                                                                                                  │
│ in parse_args:1644                                                                               │
│                                                                                                  │
│ in parse_args:1408                                                                               │
│                                                                                                  │
│ in handle_parse_result:2400                                                                      │
│                                                                                                  │
│ in process_value:2362                                                                            │
│                                                                                                  │
│ in wrapper:994                                                                                   │
│                                                                                                  │
│ in show_callback:58                                                                              │
│                                                                                                  │
│ in detect_shell:15                                                                               │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
RuntimeError: Shell detection not implemented for 'posix'
[5545] Failed to execute script '__main__' due to unhandled exception!

Similar error for --install-completion. Investigating now.

@JPHutchins
Copy link

May have resolved by adding

--hidden-import shellingham --hidden-import shellingham.posix
./dist/smpmgr --install-completion
bash completion installed in /home/jp/.bash_completions/smpmgr.sh
Completion will take effect once you restart the terminal

./dist/smpmgr --show-completion
_smpmgr_completion() {
    local IFS=$'
'
    COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   _SMPMGR_COMPLETE=complete_bash $1 ) )
    return 0
}

complete -o default -F _smpmgr_completion smpmgr

Note that specifying shellingham only didn't work for me. Since I will build for all OS, I don't like having to specify "posix" here, investigating.

@JPHutchins
Copy link

OK, I am more happy with:

--collect-submodules shellingham

In context:

pyinstaller smpmgr/__main__.py --onefile --name smpmgr --collect-submodules shellingham

Noting pyinstaller showing a good sign:

5934 INFO: Analyzing hidden import 'shellingham.nt'
5945 INFO: Analyzing hidden import 'shellingham.posix'

So it seems to be finding Windows and posix shellingham modules.

@johnthagen @wmmc88 Try this out at your convenience and see if it works for you.

I don't think that the issue should be closed until this is tested and documentation is updated.

@JPHutchins
Copy link

Confirmed that this works in Windows as well.

@matanyall
Copy link

Is there a guide for bundling typer applications with pyinstaller? I run mine and the resulting binary just exits with no output.

@JPHutchins
Copy link

Is there a guide for bundling typer applications with pyinstaller? I run mine and the resulting binary just exits with no output.

Take a look at the GitHub Workflows and portable.py here: https://github.com/intercreate/smpmgr

I am in the process of refactoring and generalizing for this series of articles: https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng

I am also working on adding "CLI app" support to briefcase: beeware/briefcase#556 (comment)

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

No branches or pull requests

4 participants