Comments (13)
There's a few things you can do to trouble shoot here.
- Make sure you're actually using the bash shell. argcomplete is designed for bash.
- What happens if you just run your script from the shell with 'python3 myscript.py' ?
- Do you get any error messages when running 'eval "$(register-python-argcomplete mcq)' in the shell?
- 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.
Thanks for your answer.
- Yes, this is the bash shell.
- The script is installed by pip through entry points.
This may be the cause of the failure indeed, since pip generates amcq
script in~/.py/bin
, and themcq
script itself don't useargparse
.
This generatedmcq
script then calls thecli.py
filemain()
function, which makes use ofargparse
.
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())
- No, I don't get any error message.
- The
mcq
script generated bypip
is already executable. Thecli.py
called bymcq
is located inside theptyx_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.
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.
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.
Hello, thanks for your interest in argcomplete.
Have you tried following the suggestions in https://github.com/kislyuk/argcomplete#debugging?
from argcomplete.
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.
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.
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 runsargcomplete.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.
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.
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.
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.
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.
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)
- Output of `argcomplete.warn` does not show HOT 4
- Generating lazy completion scripts for Bash/Zsh HOT 1
- Problem with os.get_terminal_size() and argcomplete HOT 2
- completion with a leading dash HOT 2
- Problems with trust path when packaging HOT 6
- No automatic testing on windows? HOT 2
- Python 3.13: Some tests fail when run with latest Python pre-release HOT 5
- Check recent argparse API changes for compatibility HOT 2
- completer falls back to Bash filename completion
- Python 3.12.3: Test suite fails HOT 1
- autocompletion is slow and stuck HOT 2
- Issue with File Path Autocompletion in Argparse Outside Virtual Environment HOT 6
- Inconsistent completion when using a custom validator between `fish` and `bash` HOT 1
- Consider using completer declaration other than "-default-" for zsh global completion HOT 7
- Filesystem style double tab output HOT 1
- Tab Completion does not work, if we register locally using register-python-argcomplete for a particular file only HOT 7
- Consider PYTHON_ARGCOMPLETE_OK with local activation
- zsh-related tests fail when TERM variable is set to linux HOT 1
- Remove python_argcomplete_check_easy_install_script
- Need some help figuring out how to disable this HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from argcomplete.