简体   繁体   中英

Is it possible to run a python script from the windows command prompt and pass an argument for that script at the same time?

I have a saved python script. I run this python script from the command prompt in Windows 10.

This is as simple as navigating to the directory where the script is located and then typing:

python myscript.py

and the script will run fine.

However, sometimes, I want to run this script such that a variable within that script is set to one value and sometimes to another. This variable tells the script which port to operate an API connection through (if this is relevant).

At the moment, I go into the script each time and change the variable to the one that I want and then run the script after that. Is there a way to set the variable at the time of running the script from the command prompt in Windows 10?

Or are there potentially any other efficient solutions to achieve the same flexibility at the time of running?

Thanks

The usual way to do this is with command-line arguments. In fact, passing a port number is, after passing a list of filenames, almost the paradigm case for command-line arguments.


For simple cases, you can handle this in your code with sys.argv

port = int(sys.argv[1])

Or, if you want a default value:

port = int(sys.argv[1]) if len(sys.argv) > 1 else 12345

Then, to run the program:

python myscript.py 54321

For more complicated cases—when you have multiple flags, some with values, etc.—you usually want to use something like argparse . But you'll probably want to read up a bit on typical command-line interfaces, and maybe look at the arguments of tools you commonly, before designing your first one. Because just looking at all of the options in argparse without knowing what you want in advance can be pretty overwhelming.


Another option is to use an environment variable. This is more tedious if you want to change it for each run, but if you want to set it once for an entire series of runs in a command-line session, or even set a computer-wide default, it's a lot easier.

In the code, you'd look in os.environ :

port = int(os.environ.get('MYSCRIPT_PORT', 12345))

And then, to set a port:

MYSCRIPT_PORT=54321
python myscript.py

You can combine the two: use a command-line argument if present, otherwise fall back to the environment variable, otherwise fall back to a default. Or even add a config file and/or (if you only care about Windows) registry setting. Python itself does something like three-step fallback, as do many major servers, but it may be overkill for your simple use case.

You should look at argparse. Heres an example:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-m", help='message to be sent', type=str)
args = (parser.parse_args())

print args.m

Each argument you create is saved like a dictionary so you have to call it in your code like I did with my print statement

"args.m" < ---- This specifies the argument passed you want to do stuff with

here was my input/output:

C:\Users\Vinny\Desktop>python argtest.py -m "Hi"
Hi

C:\Users\Vinny\Desktop>

More info on argparse: https://docs.python.org/3/library/argparse.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM