Giter Club home page Giter Club logo

Comments (13)

jtranquilli avatar jtranquilli commented on August 17, 2024

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'

from argcomplete.

wxgeo avatar wxgeo commented on August 17, 2024

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.

from argcomplete.

wxgeo avatar wxgeo commented on August 17, 2024

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 ?

from argcomplete.

wxgeo avatar wxgeo commented on August 17, 2024

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.

from argcomplete.

kislyuk avatar kislyuk commented on August 17, 2024

Hello, thanks for your interest in argcomplete.

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

from argcomplete.

wxgeo avatar wxgeo commented on August 17, 2024

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 ?

from argcomplete.

wxgeo avatar wxgeo commented on August 17, 2024

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).

from argcomplete.

kislyuk avatar kislyuk commented on August 17, 2024

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.

from argcomplete.

wxgeo avatar wxgeo commented on August 17, 2024

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.

from argcomplete.

wxgeo avatar wxgeo commented on August 17, 2024

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.

from argcomplete.

evanunderscore avatar evanunderscore commented on August 17, 2024

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>

from argcomplete.

wxgeo avatar wxgeo commented on August 17, 2024

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...

from argcomplete.

evanunderscore avatar evanunderscore commented on August 17, 2024

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.

from argcomplete.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.