Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an argument via command line to set an environ in Python

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?

like image 323
Sebastian Avatar asked Oct 28 '19 14:10

Sebastian


People also ask

How do you assign a command line argument to a variable in Python?

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.

How do you pass an environment variable in Python?

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.

How do I set environment variable in CMD?

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 ").


1 Answers

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/

like image 60
tmt Avatar answered Sep 23 '22 06:09

tmt