kislyuk / argcomplete

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

No completion running with `python script.py` #429

Closed JunkyByte closed 1 year ago

JunkyByte commented 1 year ago

Hello I am on intel osx, bash version 5.2.15. I installed the package with pip and run global activation.

If I run the sample script provided using ./script.py [TAB] the completion pops, while if I use python script.py [TAB] does not. What should I run to debug this?

I tried using _ARC_DEBUG but I have no output when running using python script.py [TAB].

Thanks!

flu0r1ne commented 1 year ago
JunkyByte commented 1 year ago

@flu0r1ne hello! Thanks for the help, I'm using python 3.9.16 I installed following the pr instructions, it does not fix my problem, still get [files] when I press tab calling the script with python script.py, while it works if I use it as an executable.

If I check complete I get

$ complete | grep 'argcomplete'
complete -o nospace -F _python_argcomplete gcloud
complete -o bashdefault -o default -F _python_argcomplete_global -D
complete -o nospace -F _python_argcomplete gsutil
flu0r1ne commented 1 year ago

I'm unsure the script to which you're referring. If you're referring to the test module in ./test/test_package/test_module.py, it does not contain a shebang. As a result, the operating system does not know which interpreter to use and completion will not be successful.

You would have to add it:

diff --git a/test/test_package/test_module.py b/test/test_package/test_module.py
old mode 100644
new mode 100755
index dcd26e8..e8e5047
--- a/test/test_package/test_module.py
+++ b/test/test_package/test_module.py
@@ -1,3 +1,4 @@
+#!/bin/python3
 # PYTHON_ARGCOMPLETE_OK

 from test_package import main
JunkyByte commented 1 year ago

Hi @flu0r1ne , sorry I did not have the time to reply. The script I'm using for testing is the following one

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argcomplete, argparse, requests, pprint

def github_org_members(prefix, parsed_args, **kwargs):
    resource = "https://api.github.com/orgs/{org}/members".format(org=parsed_args.organization)
    return (member['login'] for member in requests.get(resource).json() if member['login'].startswith(prefix))

parser = argparse.ArgumentParser()
parser.add_argument("--organization", help="GitHub organization")
parser.add_argument("--member", help="GitHub member").completer = github_org_members

argcomplete.autocomplete(parser)
args = parser.parse_args()

pprint.pprint(requests.get("https://api.github.com/users/{m}".format(m=args.member)).json())

I can also confirm that /usr/bin/env python is the correct version of python and has the argcomplete module installed from your PR

flu0r1ne commented 1 year ago

Both of the PRs I have contributed have been integrated into the master branch, so the latest version of the project now contains these updates. Unfortunately, I have not been able to reproduce the issue you reported when testing with my Linux setup and the script you provided.

When running the script in a Bash environment, I received the following suggestions:

 $ python test.py -
-h              --help          --member        --organization

This functionality would not have been possible in the prior version of the script, that is, the one available before the integration of my updates. Therefore, could you kindly confirm that you are using the latest version of the completion script? You should be able to corroborate this by reproducing the following:

$ cat /usr/local/etc/bash_completion.d/python-argcomplete | sha256sum
041f697635cfe040d66df061af28cfc400211e13118167c033cd764c110641d8  -

Should the hash value of your file differ from the above, I would recommend reinstalling the global completion script directly from the develop branch. If the problem persists despite this, we may need to conduct a more comprehensive investigation into this issue.

JunkyByte commented 1 year ago

Hi @flu0r1ne, I tried to use the latest version but could not fix the issue. Following the https://github.com/kislyuk/argcomplete#common-problems I used complete -r python and it fixed the problem, allowing the argcomplete to work correctly.

Thank you for your time and help