kislyuk / argcomplete

Python and tab completion, better together.
https://kislyuk.github.io/argcomplete/
Apache License 2.0
1.39k stars 129 forks source link

Document ephemeral vs. permanent completion activation #450

Open wxgeo opened 9 months ago

wxgeo commented 9 months ago

Hi,

Thanks for this great library.

When trying to activate completion for my script, nothing happens: no error raised, but when starting a new terminal, completion simply doesn't work. I can enable completion globally yet (using activate-global-python-argcomplete).

Command launched in bash: eval "$(register-python-argcomplete mcq)" (where mcq is my own command).

My configuration:

My script: https://www.toptal.com/developers/paste-gd/ZgOSfr3n

I first tried to activate completion through a python script, then directly in bash with eval "$(register-python-argcomplete mcq)", but no success. :(

I'd like to activate autocompletion only for the command mcq, and not for all python scripts.

jtranquilli commented 9 months ago

There's a few things you can do to trouble shoot here.

  1. Make sure you're actually using the bash shell. argcomplete is designed for bash.
  2. What happens if you just run your script from the shell with 'python3 myscript.py' ?
  3. Do you get any error messages when running 'eval "$(register-python-argcomplete mcq)' in the shell?
  4. You can try converting your script to an executable file with 'chmod +x /path/to/mcq' after you do this you can run your script with the command './your_script.py'
wxgeo commented 9 months ago

Thanks for your answer.

  1. Yes, this is the bash shell.
  2. The script is installed by pip through entry points. This may be the cause of the failure indeed, since pip generates a mcq script in ~/.py/bin, and the mcq script itself don't use argparse. This generated mcq script then calls the cli.py file main() function, which makes use of argparse.

This is the script automatically generated by pip:

#!/home/nicolas/.py/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from ptyx_mcq.cli import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())
  1. No, I don't get any error message.
  2. The mcq script generated by pip is already executable. The cli.py called by mcq is located inside the ptyx_mcq package and can't be called directly for now (ImportError: attempted relative import with no known parent package), though I could convert relative imports to absolute ones if it helps.
wxgeo commented 9 months ago

I think I must in fact use option --external-argcomplete-script, since the mcq command is only a wrapper generated by pip for the cli.py file.

So, I tried eval "$(register-python-argcomplete --external-argcomplete-script /home/nicolas/prog/ptyx-mcq/ptyx_mcq/cli.py mcq)", without more success.

Having no feedback when executing the command in bash makes the debugging difficult... :(

Any idea or suggestion ?

wxgeo commented 9 months ago

I converted my script to not use absolute imports, so python3 cli.py works now.

I also tried to change directory, and execute eval "$(register-python-argcomplete --external-argcomplete-script cli.py mcq)" in the directory containing cli.py, but this doesn't work either.

kislyuk commented 9 months ago

Hello, thanks for your interest in argcomplete.

Have you tried following the suggestions in https://github.com/kislyuk/argcomplete#debugging?

wxgeo commented 9 months ago

Thanks a lot for your suggestion. I've tried it already, but tried again in case I missed something.

While I'm a long time python and Linux user, I've very few notions of bash, but I suppose the following should be enough ?

$ export _ARC_DEBUG=1
$ eval "$(register-python-argcomplete  --external-argcomplete-script cli.py mcq)"

If so, there's still absolutely no message displayed in the shell.

No message either when opening a new shell after that, executing export _ARC_DEBUG=1 in it and trying autocompletion on mcq command then.

Did I make something wrong, or is there something else I can try ?

wxgeo commented 9 months ago

I noticed also that I can put anything as --external-argcomplete-script argument, there's never any message displayed.

(However, if I use an invalid option name like --invalid-option, then I get the usual argparse python message reminding the register-python-argcomplete arguments, as expected).

kislyuk commented 9 months ago

You have to run all of those commands in the same shell:

eval "$(register-python-argcomplete  --external-argcomplete-script cli.py mcq)"
wxgeo commented 9 months ago

Thanks a lot for your help !

ensure that this file is an executable

OK, this was indeed the reason it failed. mcq was executable, but cli.py was not (it could be run using python3 cli.py, but ./cli.py failed to launch).

After executing chmod u+x cli.py, the command eval "$(register-python-argcomplete --external-argcomplete-script /home/nicolas/prog/ptyx-mcq/ptyx_mcq/cli.py mcq)" is working now.

Thanks again !

May I suggest you a few improvements, by the way ?

It would be really nice from register-python-argcomplete to provide a feedback when --external-argcomplete-script provided path is incorrect, with the reason of the failure : invalid path, not a python file, file not executable, and so on.

I thought _ARC_DEBUG=1 would help, but it seems to be only useful to debug the completion itself once installed, and not to debug the completion installation process.

wxgeo commented 9 months ago

For anyone else reading this thread in the future, one point not obvious from the doc, though it makes sense, is that the completion will only be enabled in the current shell.

So, you'll have to append the command eval "$(register-python-argcomplete --external-argcomplete-script /home/nicolas/prog/ptyx-mcq/ptyx_mcq/cli.py mcq)" to the ~/.bashrc file.

Alternatively, to avoid launching a python process at every shell startup, one may execute register-python-argcomplete --external-argcomplete-script /home/nicolas/prog/ptyx-mcq/ptyx_mcq/cli.py mcq >> ~/.mcq_completion and append source /home/nicolas/.mcq_completion to the ~/.bashrc file.

evanunderscore commented 6 months ago

You shouldn't need to use --external-argcomplete-script - there is special logic in global completion to unwrap the pip wrappers and work out if they point to something that can be completed, but eval "$(register-python-argcomplete mcq)" should unconditionally attempt to invoke mcq as a completer.

Can you try this and post the output please?

mcq --help  # or something similar that shows your program is actually executable as `mcq`
eval "$(register-python-argcomplete mcq)"
mcq <tab>
wxgeo commented 6 months ago

Interesting, this works now without using --external-argcomplete-script.

The initial failure may then have been caused by the fact that, initially, mcq.py couldn't be called directly (it had to be called through the wrapper mcq).

It's the only explanation I can see...

evanunderscore commented 6 months ago

If your script was executable as mcq then that is all that should matter; argcomplete literally executes mcq in order to generate the completions. It's likely there was some other installation or configuration problem, but I'm glad it's working now.

For anyone else reading this thread in the future, one point not obvious from the doc, though it makes sense, is that the completion will only be enabled in the current shell.

It probably wouldn't hurt to add this to the doc, since I agree it's not currently obvious that it only lasts for the life of the current shell.