1

I want to be able to run the following command:

sh -c "python -c "import sys;sys.platform""

however I am failing to do so with subprocess

I have tried the following but

subprocess.check_output(["sh", "-c", ["python", "-c",  '"import sys; print sys.platform"']])

I get the following output:

sh: python-cimport: command not found
File "<string>", line 1
    "import
          ^

2 Answers 2

2

In the order of preference (how to print the platform info):

#!/usr/bin/env python
import platform

print(platform.platform())

If you want to run it as a separate process:

#!/usr/bin/env python
import subprocess
import sys

subprocess.check_call([sys.executable or 'python', '-m', 'platform'])

If you want to run in a shell:

#!/usr/bin/env python
import subprocess

subprocess.check_call('python -m platform', shell=True)

On POSIX, it is equvalent to:

subprocess.check_call(['/bin/sh', '-c', 'python -m platform'])

Your specific command:

subprocess.check_call(['/bin/sh', '-c', 
                       "python -c 'import sys; print(sys.platform)'"])
Sign up to request clarification or add additional context in comments.

Comments

1

Your quotes are getting tangled up with each other. Try:

sh -c 'python -c "import sys; print sys.platform"'

Or, if you're trying to call it from inside another python program, perhaps you mean to say this...

subprocess.check_output(['python', '-c', 'import sys; print sys.platform'])

Or is there a great reason for trying to nest this inside a shell?

1 Comment

The reason is I am running this under cygwin and the script which I am about to call with subprocess is incompatible with windows and msys/mingw. I wanted to know whether or not the python used is cygwin based or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.