When you call subprocess.Popen
you can pass either a string or a list for the command to be run. If you pass a list, the items should be split in a particular way.
In your case, you need to split it something like this:
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)
This is because if you pass in a list, Popen
assumes you have already split the command line into words (the values that would end up in sys.argv
), so it doesn't need to.
The way you're calling it, it will try to run a binary called "python mytool.py -a", which isn't what you meant.
The other way to fix it is to join all of the words into a string (which Popen
will then split up - see subprocess.list2cmdline
). But you're better off using the list version if possible - it gives simpler control of how the commandline is split up (if arguments have spaces or quotes in them, for example) without having to mess around with quoting quote characters.