Try separating the values with commas:

subprocess.call(['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, '-u', _userid, _username])

See http://docs.python.org/library/subprocess.html#subprocess.call - It takes an array where the first argument is the program and all other arguments are passed as arguments to the program.

Also don't forget to check the return value of the function for a zero return code which means "success" unless it doesn't matter for your script if the user was added successfully or not.

Answer from ThiefMaster on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how can i pass arguments to subprocess.call or subprocess.run for execution in ps?
r/learnpython on Reddit: How can I pass arguments to subprocess.call or subprocess.run for execution in PS?
July 11, 2021 -

Hello,

I'm trying to launch a powershell script via subprocess.call or subprocess.run, but I'm having trouble with how to specify the arguments for a function in powershell.

For example, in a powershell script I have a function Foo which accepts a string. With subprocess I would like to launch this script and pass an argument to Foo.

How can I do that?

In order to simply call the script I managed to do it in the following way:

if is_admin():
    # If launched as admin - good
    cmd = ["PowerShell", "-ExecutionPolicy", "Unrestricted", "-File", absolute_path]
    ec = subprocess.call(cmd)
    print("Powershell returned: {0:d}".format(ec))
else:
    # If not admin, relaunch as admin
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
Discussions

How to pass arguments in python subprocess? - Stack Overflow
As explained here: https://docs.python.org/3/library/subprocess.html, you can add in the array every argument you would normally pass in a command-line. More on stackoverflow.com
🌐 stackoverflow.com
python - How to pass arguments to subprocess - Stack Overflow
I want run a process called client.sh in gnome-terminal via python script and want to pass the arguments as input to execute it. Below is my code import os import subprocess import time from subp... More on stackoverflow.com
🌐 stackoverflow.com
How do I pass arguments to another function with subprocess.Popen?
Don't do that. Use import. More on reddit.com
🌐 r/learnpython
5
1
February 27, 2022
Python subprocess arguments - Stack Overflow
Each argument needs to be separated when subprocess.call is used with shell=False (the default). You can also specify shell=True and give the whole command as a single string, but this is not recommended due to potential security vulnerabilities. You should not need to use string formatting where you have "%s" % url. If url is a string, pass ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
3 days ago - However, this can only be done if not passing arguments to the program. ... It may not be obvious how to break a shell command into a sequence of arguments, especially in complex cases. shlex.split() can illustrate how to determine the correct tokenization for args: >>> import shlex, subprocess >>> command_line = input() /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'" >>> args = shlex.split(command_line) >>> print(args) ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) # Success!
🌐
Gilad Naor
blog.giladnaor.com › 2009 › 09 › python-subprocess-and-multiple.html
Python, Subprocess and Multiple Arguments - Gilad Naor
September 26, 2009 - 1 **#!/bin/env python** 2 import ... output · If you want to pass arguments that are passed with quotes in the shell, then just pass them as a single list item, without the quotes....
🌐
Python Morsels
pythonmorsels.com › running-subprocesses-in-python
Running subprocesses in Python - Python Morsels
March 6, 2025 - import subprocess subprocess.run(["git", "branch", "--format=%(refname:short)"]) This list indicates the process to be run, and the arguments to pass to that process.
🌐
Stack Overflow
stackoverflow.com › questions › 49706015 › how-to-pass-arguments-to-subprocess › 49707454
python - How to pass arguments to subprocess - Stack Overflow
process = subprocess.Popen(['sudo gnome-terminal -x ./client.sh', '1'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
🌐
Python
docs.python.org › 3.4 › library › subprocess.html
17.5. subprocess — Subprocess management — Python 3.4.10 documentation
June 16, 2019 - The input argument is passed to Popen.communicate() and thus to the subprocess’s stdin. If used it must be a byte sequence, or a string if universal_newlines=True.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how do i pass arguments to another function with subprocess.popen?
r/learnpython on Reddit: How do I pass arguments to another function with subprocess.Popen?
February 27, 2022 -

Hi! I use Subprocess.Popen to specificly call a function from another python module/script.

This works:

subprocess.Popen(["python", "-c",
"import " + "main" + ";" + "main" + "." + "func" + "()"])

However, say I have some global variables from the python script I call this function, how would I pass these into subprocess.Popen? Like this:

arg1 = 2, arg2 = 4

subprocess.Popen(["python", "-c",
"import " + "main" + ";" + "main" + "." + "func" + "(arg1, arg2)"])

Above example does not work. How can I make it work?

🌐
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - The subprocess.run() method takes several arguments, some of which are: args: The command to run and its arguments, passed as a list of strings.
🌐
Python 101
python101.pythonlibrary.org › chapter19_subprocess.html
Chapter 19 - The subprocess Module — Python 101 1.0 documentation
In this code example, we create an args variable to hold our list of arguments. Then we redirect standard out (stdout) to our subprocess so we can communicate with it. The communicate method itself allows us to communicate with the process we just spawned. We can actually pass input to the ...
🌐
Linux Mint Forums
forums.linuxmint.com › board index › interests › programming & development
[SOLVED] Python: how to pass argument containing blank space with subprocess? - Linux Mint Forums
September 19, 2024 - You shouldn't need to quote DejaVu Sans, the command isn't running in a shell. subprocess.run passes each argument in the list separately to the invoked command, without word splitting.
🌐
GitHub
hplgit.github.io › primer.html › doc › pub › tech › ._tech-solarized012.html
Technical topics
import subprocess cmd = 'python myprog.py 21 --mass 4' failure = subprocess.call(cmd, shell=True) # or failure = subprocess.call( ['python', 'myprog.py', '21', '--mass', '4']) The output of an operating system command can be stored in a string object: try: output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: print 'Execution of "%s" failed!\n' % cmd sys.exit(1) # Process output for line in output.splitlines(): ... The stderr argument ensures that the output string contains everything that the command cmd wrote to both standard output and standard error.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Pass variable to subprocess.Popen - Python
gkreidl wrote:You pass the string "tlog" and not the variable. Remove the double quotes. Are you sure ? Have you tried your solution for yourself ? It doesn't seem to work for me ... >>> import subprocess >>> n = 10 >>> subprocess.Popen(["./args","hello",n]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/subprocess.py", line 710, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child raise child_exception TypeError: execv() arg 2 must contain only strings You need
🌐
Python Forum
python-forum.io › thread-36920.html
Pass variable to subprocess
Hi all, I want to pass a variable to this command : VARIABLE=12 v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);Ho
🌐
Quora
quora.com › How-do-you-pass-command-line-arguments-to-a-Python-script-using-the-subprocess-module
How to pass command line arguments to a Python script using the subprocess module - Quora
Answer: You can very well use the advantage of the builtin module sys and the attribute argv , which has enough capabalities to pass the command line arguments to the module . PFB the example..
🌐
DevGenius
blog.devgenius.io › mastering-pythons-subprocess-run-with-smart-parameter-mapping-e08cfe78378d
Mastering Python’s subprocess.run() with Smart Parameter Mapping | by Anzalo Quin | Dev Genius
May 28, 2025 - subprocess.run(["/path/to/script.sh", "arg1", "arg2", "arg3"]) ... Positional arguments can quickly become a maintenance nightmare. By converting your arguments into key-value pairs, you eliminate ambiguity and make your scripts much easier ...
🌐
CopyProgramming
copyprogramming.com › howto › python-subprocess-run-with-arguments-in-python
Python subprocess.run with Arguments: Complete Guide 2026
December 29, 2025 - When you need basic command execution with output capture, timeout handling, and error checking, subprocess.run() is the ideal choice. The args parameter specifies the command to execute. It should be passed as a list of strings (recommended) rather than a single string. Each element in the ...