Textualize / trogon

Easily turn your Click CLI into a powerful terminal application
MIT License
2.5k stars 54 forks source link

Trogon script execution fails with FileNotFoundError: [Errno 2] No such file or directory #70

Open arunabha opened 11 months ago

arunabha commented 11 months ago

Running the nogroup_demo example fails with FileNotFoundError: [Errno 2] No such file or directory

The cwd and the program_name arguments to the os.execvp call seem to be correct, however the run still fails. The cli version works fine though.

I added a debug log just before the call to execvp listing the following prog_name = nogroup_demo.py, args = ['nogroup_demo.py', 'add', '--category', 'work', 'foo'] app_name = nogroup_demo.py cwd = /Users/arunabhaghosh/dev/import_migrate_data

---- Details--------------------------

Running nogroup_demo.py add --category work foo
prog_name = nogroup_demo.py, args = ['nogroup_demo.py', 'add', '--category', 'work', 'foo'] app_name = nogroup_demo.py cwd = /Users/arunabhaghosh/dev/import_migrate_data
Traceback (most recent call last):
  File "/Users/arunabhaghosh/dev/import_migrate_data/nogroup_demo.py", line 52, in <module>
    add()
  File "/Users/arunabhaghosh/dev/import_migrate_data/venv/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/arunabhaghosh/dev/import_migrate_data/venv/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/arunabhaghosh/dev/import_migrate_data/venv/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/arunabhaghosh/dev/import_migrate_data/venv/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/arunabhaghosh/dev/import_migrate_data/venv/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/arunabhaghosh/dev/import_migrate_data/venv/lib/python3.11/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/arunabhaghosh/dev/import_migrate_data/venv/lib/python3.11/site-packages/trogon/trogon.py", line 297, in wrapped_tui
    Trogon(app, app_name=name, command_name=command, click_context=ctx).run()
  File "/Users/arunabhaghosh/dev/import_migrate_data/venv/lib/python3.11/site-packages/trogon/trogon.py", line 265, in run
    os.execvp(program_name, arguments)
  File "<frozen os>", line 574, in execvp
  File "<frozen os>", line 616, in _execvpe
  File "<frozen os>", line 607, in _execvpe
FileNotFoundError: [Errno 2] No such file or directory
mgielda commented 11 months ago

I can confirm the bug. Also, if you don't select anything the demo fails with a different error, but the selection is not marked as compulsory. This is probably less of an issue but still got me confused initially.

Anyway the No such file or directory also occurs for me in my attempts to use this project in another practical app.

iandennismiller commented 8 months ago

@mgielda by any chance are you running trogon inside a virtual environment? I noticed that trogon echos a preview of the command it will run when ctrl-R is pressed. If that preview command were entered directly on the command line, would the environment be set correctly so the script would be in the path? In my case, the answer was no.

I solved this in my situation by activating the virtual environment in my shell before launching trogon. Previously, I had been launching in a different way that avoided the shell activation part of venv.

42sol-eu commented 8 months ago

I have the same issue. Using Ctrl+R after setting values.

Could it be connected to trogon calling the python script directly instead of using `python ./{script_name}.py ?

I am not in a virtual environment and using the default python. Running on Windows 10.

scripts main *% = $ python .\trogon_demo.py tui 

Running trogon_demo.py add test
Traceback (most recent call last):
  File "C:\_projects\onboarding_template\scripts\trogon_demo.py", line 89, in <module>
    cli(obj={})
  File "C:\python\v311\Lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python\v311\Lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\python\v311\Lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python\v311\Lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python\v311\Lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python\v311\Lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python\v311\Lib\site-packages\trogon\trogon.py", line 296, in wrapped_tui
    Trogon(app, app_name=name, command_name=command, click_context=ctx).run()
  File "C:\python\v311\Lib\site-packages\trogon\trogon.py", line 264, in run
    os.execvp(program_name, arguments)
  File "<frozen os>", line 574, in execvp
  File "<frozen os>", line 616, in _execvpe
  File "<frozen os>", line 607, in _execvpe
FileNotFoundError: [Errno 2] No such file or directory
42sol-eu commented 8 months ago

I confirm that extending trogon.py in the run function like the following solve my issue

                    arguments = [f"./{program_name}", *self.post_run_command]
                    print(f"{program_name=}, {arguments=}")
                    os.execvp("python", arguments)

meaning:

It strangely does not work on the demo.py from the examples folder.

Maybe I have to recheck after a good nights sleep ;-)

antonio-antuan commented 5 months ago

these changes solves the issue for me:

arguments = [*split_app_name, *self.post_run_command]
program_name = os.path.abspath(program_name)
arguments = [sys.executable, f"{program_name}", *self.post_run_command]
os.execvp(sys.executable, arguments)