I'm using python 3.7 and os library. I have to run a battery of tests on both STAGE and TEST environments. Currently the code sets the environment to STAGE
ENVIRONMENT = os.getenv('ENV', 'STAGE')
but I want it to be set by passing an argument via command line. Anyone?
To access command-line arguments from within a Python program, first import the sys package. You can then refer to the full set of command-line arguments, including the function name itself, by referring to a list named argv. In either case, argv refers to a list of command-line arguments, all stored as strings.
To set and get environment variables in Python you can just use the os module: import os # Set environment variables os. environ['API_USER'] = 'username' os. environ['API_PASSWORD'] = 'secret' # Get environment variables USER = os.
To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").
In case of a command line of a UNIX shell you can set the env variable as part of your command:
$ ENV=STAGE pytest ./tests/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With