Open wxgeo opened 9 months ago
There's a few things you can do to trouble shoot here.
Thanks for your answer.
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())
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.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 ?
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.
Hello, thanks for your interest in argcomplete.
Have you tried following the suggestions in https://github.com/kislyuk/argcomplete#debugging?
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 ?
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).
You have to run all of those commands in the same shell:
eval "$(register-python-argcomplete --external-argcomplete-script cli.py mcq)"
/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
mcq ... <TAB>
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.
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.
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>
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...
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.
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)"
(wheremcq
is my own command).My configuration:
source ~/.py/bin/activate
in my.bashrc
, where.py
is my venv)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.