import subprocess import sys result = subprocess.run( [sys.executable, "-c", "import sys; print(repr(sys.argv[1]))", "a name; with spaces"], check=True, capture_output=True, text=True, timeout=5, ) print(result.stdout.strip()) # 'a name; with spaces'