Home > Enterprise >  Can Python be invoked against a script file with a parameter-name for the script filepath?
Can Python be invoked against a script file with a parameter-name for the script filepath?

Time:02-22

Does anyone know if the python3 command on Linux can have some sort of SOME_NAMED_OPTION=filename.py after it rather than just calling python3 filename.py?

I have a job scheduling tool I'd like to execute a Python script with that's kind of dumb.

It can only run Linux CLI commands commandname param1 param2 param3 or as commandname AAA=param1 BBB=param2 CCC=param3.

There's an existing business convention of putting filename.py as param #1 and then just making sure your script has a lot of comments about what the subsequent numerically ordered sys.argv list members mean, and you set the scheduling tool into its first mode, so it runs python3 filename.py world mundo monde, but it'd be awesome to be able to name the scheduling tool's parameters #2 so I can write more human-friendly Python programs.

With python3 -h I'm not finding a way to give a parameter-name to filename.py, but I thought I'd see if anyone else had done it and I'm just missing it.

It'd be cool if I could have my scheduling tool run the a command more like python3 --scriptsource=filename.py --salut=monde --hola=mundo --hello=world and then write filename.py to use argparse to grab hola's, hello's, and salut's values by name instead of by position.

CodePudding user response:

You can create a python file to be executed as a script like in the example bellow:

    #!/usr/bin/python3  # path to python in your system
    
    print('This is a script!')

If your python file is named test.py you can execute as ./test.py if the file has permission to be executed. To grant execution permission to your file use the command chmod x test.py

CodePudding user response:

Using the Shebang as suggested by @daniboy000 is the preferred method for doing this, but if for some reason you can't do that, you should be able to do python3 mycommand --arg1 --arg2=x

  • Related