Passing arguments to an entry point python script using argparser
Asked Answered
C

2

9

I am looking to pass a user entered arguments from the command line to an entry point for a python script. Thus far I have tried to used argparse to pass the arguments from the command line to the test.py script. When I try to pass the arguments they are not recognised and I recieve the following error.

load_entry_point('thesaurus==0.1', 'console_scripts', 'thesaurus')()
TypeError: find_synonym() missing 1 required positional argument: 'argv'

I have looked at other examples on here but have not been able to get any of the solutions to work.

def main(argv):
    if argv is None:
        argv = sys.argv

    parser = argparse.ArgumentParser(description='Enter string')
    parser.add_argument('string', type=str, help='Enter word or words', nargs='*')
    args = parser.parse_args(argv[1:])
    print(args)

if __name__ == "__main__":
    sys.exit(main(sys.argv))

My setup.py script entry point looks like the following

setup(entry_points={'console_scripts': ['test=test_folder.main:main']})

What I expect to happen is similar to when I run python main.py main foo. Which will successfully print out hello when it is passes to the function.

Celestaceleste answered 2/5, 2020 at 19:29 Comment(2)
If you are using argparse, why are you using sys.argv?Wrinkle
@OldWinterton, using argv like this gives some flexibility when testing main. This script runs fine when called in a conventional way.Rudderpost
C
7

So it works now when I remove the the arguments from the function.

 def main():          
        parser = argparse.ArgumentParser(description='Enter string')
        parser.add_argument('string', type=str, help='Enter word or words', nargs='*')
        args = parser.parse_args()
        print(args.string)

if __name__ == "__main__":
    main()

I believe that it might work with the sys.arv used in the first example, as my actual problem was that when I re-downloaded this script from GitHub using.

pip install git+ URL

The script was not updating so the error persisted, This required deleting all the files related to the GitHub repository and re-installing it using the same pip command.

Celestaceleste answered 7/5, 2020 at 13:47 Comment(1)
I provided a full working example based on this answer in a blog post for anyone interested hughesadam87.medium.com/…Fluoroscopy
Q
3

To get the best of both worlds, main() should accept argv (which makes it easier to test, as you observed). However, you need a default to use it as an entrypoint so it can be invoked with no args. That would look like this:


def main(argv=sys.argv):
    parser = argparse.ArgumentParser(description='Enter string')
    parser.add_argument('string', type=str, help='Enter word or words', nargs='*')
    args = parser.parse_args(argv[1:])
    print(args)

Now it is testable and you can still use it as an entrypoint.

Queri answered 26/5, 2022 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.