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

Document ephemeral vs. permanent completion activation #450

Open
wxgeo opened this issue Sep 26, 2023 · 13 comments
Open

Document ephemeral vs. permanent completion activation #450

wxgeo opened this issue Sep 26, 2023 · 13 comments

Comments

@wxgeo
Copy link

wxgeo commented Sep 26, 2023

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:

  • python 3.11.2 in a virtual environment (I run source ~/.py/bin/activate in my .bashrc, where .py is my venv)
  • argcomplete 3.1.2
  • bash 5.2.15 (on Debian 12)

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
Copy link

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
Copy link
Author

wxgeo commented Sep 26, 2023

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
Copy link
Author

wxgeo commented Sep 27, 2023

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
Copy link
Author

wxgeo commented Sep 28, 2023

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
Copy link
Owner

kislyuk commented Oct 1, 2023

Hello, thanks for your interest in argcomplete.

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

@wxgeo
Copy link
Author

wxgeo commented Oct 1, 2023

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
Copy link
Author

wxgeo commented Oct 1, 2023

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
Copy link
Owner

kislyuk commented Oct 1, 2023

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

eval "$(register-python-argcomplete  --external-argcomplete-script cli.py mcq)"
  • this sets up bash to use cli.py to retrieve completions for mcq. Note that cli.py must be an executable in your PATH (you should test this by running "cli.py" in your shell). Alternatively you can use the full path as in your other example (/home/nicolas/prog/ptyx-mcq/ptyx_mcq/cli.py), but in either case, ensure that this file is an executable that runs argcomplete.autocomplete() after setting up the argparse parser.
export _ARC_DEBUG=1
  • this tells argcomplete to emit debug output to the terminal when it runs.
mcq ... <TAB>
  • after pressing tab, you should see debug output from the argcomplete hook.

@wxgeo
Copy link
Author

wxgeo commented Oct 1, 2023

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
Copy link
Author

wxgeo commented Oct 2, 2023

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
Copy link
Collaborator

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
Copy link
Author

wxgeo commented Dec 10, 2023

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
Copy link
Collaborator

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.

@kislyuk kislyuk changed the title I can't activate completion for my script Document ephemeral vs. permanent completion activation Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants