Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Running Bash commands in Python

On my local machine, I run a python script which contains this line

bashCommand = "cwm --rdf test.rdf --ntriples > test.nt"
os.system(bashCommand)

This works fine.

Then I run the same code on a server and I get the following error message

'import site' failed; use -v for traceback
Traceback (most recent call last):
File "/usr/bin/cwm", line 48, in <module>
from swap import diag
ImportError: No module named swap

So what I did then is I inserted a print bashCommand which prints me than the command in the terminal before it runs it with os.system().

Of course, I get again the error (caused by os.system(bashCommand)) but before that error it prints the command in the terminal. Then I just copied that output and did a copy paste into the terminal and hit enter and it works...

Does anyone have a clue what's going on?

Answer*

Draft saved
Draft discarded
Cancel
12
  • 5
    For a more detailed exposition of how you might avoid calling Python as a subprocess, see this answer on a tangentially similar question. Commented Sep 8, 2018 at 10:06
  • 11
    it boggles my mind that I had to post a new answer to such a basic question in order to show how to run the command from the question idiomatically. Your answer is long but I don't see such example. Unrelated: avoid cargo-culting. If check_call() works in your case, use it. I had to fix a code that used run() blindly. Missing check=True caused a bug which would be avoided if check_call were used — "check" is in the name, you can't lose it —it is the correct default: don't ignore errors silently. I didn't read further. Commented Nov 1, 2018 at 19:47
  • 3
    @jfs Thanks for the feedback, I was in fact planning to add a section about Bash vs sh but you beat me to it. I'm trying to spell out the specifics in enough detail to help beginners for whom these pitfalls are not obvious so that does get a bit longwinded. Yours should be quite sufficient otherwise; +1 Commented Nov 2, 2018 at 5:13
  • 1
    Does stderr/stdout = subprocess.PIPE have a higher performance overhead than the default settings? Commented Apr 5, 2019 at 3:02
  • 3
    @Stringers I have not tested, but I don't see why it should. If you connect those pipes to something which does some processing, then of course that processing needs to oe accouted for; but it doesn't happen in the pipe itself. The default is to not capture stdout or stderr at all, i.e. whatever gets printed there is out of Python's visibility and control, just like with os.system(). Commented Apr 5, 2019 at 3:52

default

AltStyle によって変換されたページ (->オリジナル) /