#1 Use subprocess.run
Closed: Fixed None Opened 8 years ago by ralph.

Instead of os.system. It's python3 only and supposedly nice.


Well, now that I go looking for it, I can't find it:

❯ python3 -c 'import subprocess; print(subprocess.run)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'run'

This was raised by the reviewer in the package review.

The run() function was added in Python 3.5; if you need to retain compatibility with older versions, see the Older high-level API section.

Yeah, you might want to use subprocess.check_call if you need compatibility with older Pythons.
Spectool has a compatibility reimplementation: https://pagure.io/spectool/blob/master/f/spectool#_38.

Hm, so.. what's wrong with os.system(...)?

For one it calls (or might call) a shell. This should be avoided.
Also, what I was objecting to was the merging of user specified arguments without any quoting. There's no need to merge the arguments into a command line.

On second thought, subprocess.run might not be what you want, especially that it's rather new. subprocess.call or subprocesses.check_call might be better:

def run(cmd):
    click.echo('  $ ' + ' '.join(cmd))
    return subprocess.call(cmd)

def clone(name):
    url = repo_url(name, ssh=True, git=True)
    return run(['git', 'clone', url, name.split('/')[-1]])

Login to comment on this ticket.

Metadata