Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (47.6k points)

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?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code for running bash commands in Python:-

bashCommand = "cwm --rdf test.rdf --ntriples > test.nt" 

import subprocess 

process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE) 

output, error = process.communicate()

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Browse Categories

...