Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to an entry point python script using argparser

Tags:

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.

like image 484
steve2020 Avatar asked May 02 '20 19:05

steve2020


People also ask

How do you pass arguments to Argparse Python?

After importing the library, argparse. ArgumentParser() initializes the parser so that you can start to add custom arguments. To add your arguments, use parser. add_argument() .

How do you pass arguments to a Python script?

In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys. argv ) will store all the information in the command line entry and can be accessed inside the Python script. Python's getopt module can also be used to parse named arguments.

How do you give a Python script a command line input?

If so, you'll need to use the input() command. The input() command allows you to require a user to enter a string or number while a program is running. The input() method replaced the old raw_input() method that existed in Python v2. Open a terminal and run the python command to access Python.


1 Answers

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.

like image 61
steve2020 Avatar answered Sep 30 '22 19:09

steve2020