How to pass variables to Python subprocess.Popen?

Sometimes, we want to pass variables to Python subprocess.Popen.

In this article, we’ll look at how to pass variables to Python subprocess.Popen.

How to pass variables to Python subprocess.Popen?

To pass variables to Python subprocess.Popen, we cann Popen with a list that has the variables we want to include.

For instance, we write

command = [
    "python",
    "mytool.py",
    "-a",
    servers[server]["address"],
    "-x",
    servers[server]["port"],
    "-p",
    servers[server]["pass"],
    "some",
    "additional",
    "command",
]
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

to call Popen with the command list that has some static and dynamic command arguments.

Conclusion

To pass variables to Python subprocess.Popen, we cann Popen with a list that has the variables we want to include.