11

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?

Sebastian
  • 111
  • 1
  • 1
  • 3
  • 1
    Possible duplicate of [How to read/process command line arguments?](https://stackoverflow.com/questions/1009860/how-to-read-process-command-line-arguments) – Kraay89 Oct 28 '19 at 15:01
  • 2
    It doesn't set it to `STAGE`, it is **defaulting** to `STAGE` if you have not set `ENV` to anything. See [`os.getenv(...)`](https://docs.python.org/3/library/os.html#os.getenv) – smac89 Oct 28 '19 at 15:01
  • 1
    How are you running the code? From the commandline or from python itself? What I mean by "python" is, are you having to use the subprocess module to run the script? – smac89 Oct 28 '19 at 15:05
  • Possible duplicate of https://stackoverflow.com/questions/36141024/how-to-pass-environment-variables-to-pytest – SilentGuy Oct 28 '19 at 18:47

3 Answers3

16

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/

tmt
  • 7,611
  • 4
  • 32
  • 46
1

Setting the environment variable of liking is rather simple, giving the name and the value for a single environmental variable would be something along these lines:

Using the command line

import os
import sys

var_name = sys.argv[1]
var_value = sys.argv[2]

os.environ[var_name] = var_value

Just execute your script from the command line as:

my.script ENV STAGE

This can be verified as follows within python:

var_name in os.environ #python3
os.environ.has_key(var_name) #python2

Interactively

Using a while construct in the script

import os

while True:

    env_var = input("Enter ENV (name:value), type 'done' to exit: ")) #enter var:value

    if env_var.lower() == "done":

        break

    try:

        var, val = env_var.split(":")

    except:

        print ("Wrong input format! name:value required.")

        continue

    os.setenv[var] = val
Fourier
  • 2,795
  • 3
  • 25
  • 39
  • 1
    This is not an answer to the question. OP is asking about environment variables not command line arguments – smac89 Oct 28 '19 at 15:07
  • 2
    @smac89 are you sure? OP said _I want it [the environment variable] to be set by passing an **argument via command line**_. IMHO this answer **is** an answer to the OPs question. – jofrev Oct 28 '19 at 15:31
  • 1
    @jofrev, there is a difference between **_setting environment variable on the command line_** for a program and passing **_command line arguments_** to a program, see the [other answer](https://stackoverflow.com/a/58593432/2089675) for one of the ways to do so in a UNIX environment – smac89 Oct 28 '19 at 15:33
  • 1
    @smac89, I agree with there being a difference between those two things. Maybe I'm missing something but in my understanding what the OP wanted to do is to pass some arguments to a python program/script and modify the environment according to them. Isn't this what the original answer did? – jofrev Oct 28 '19 at 15:42
  • 3
    @jofrev it makes a difference which you use, because only one of them will populate [`os.environ`](https://docs.python.org/3.7/library/os.html#os.environ) with the right value for a given environment variable name. This answer is doing it manually by asking the user for the value via the command line arguments, and then manually populating the environment variables. Also OP is asking to be able to do `ENVIRONMENT = os.getenv('ENV', 'STAGE')`, and get the value for the `ENV` variable, not asking the user for a value – smac89 Oct 28 '19 at 15:55
  • 1
    @jofrev I get the confusion, but my point is that passing something via command line is not restricted to just command line _arguments_ and depending on that you want to pass (in this case environmental variables), command line arguments are not the way to go about it. Sure you can do it with command line arguments (as this answer shows), but you are simply reinventing the already available mechanism for doing so – smac89 Oct 28 '19 at 16:03
  • 1
    @smac89 I guess we were then just coming from a different interpretation/assumption of what the OP (actually) wanted. Thanks for clarifying your point of view!! – jofrev Oct 28 '19 at 16:10
0

After "python myfile.py", just type the parameters delimited by spaces, and in the code, you can do the following:

import sys

nameOfScript = sys.argv[0]
commandLineArgs = sys.argv[1:]
commandLineArgsAsStr = str(sys.argv)
numArgs = len(sys.argv)