sh command in python fails
I am a beginner in python. I got these scripts from IP vendor.
cmd = "sh compile_regression_vcs.sh"
try:
self.agent.run_commands(cmd, pwd=self.agent.test_dir, extra_env=env)
except TestException as e:
s = traceback.format_exc()
serr = "there were errors:\n%s\n" % (s)
开发者_JS百科 self.logger.info(serr)
raise TestException("Could not compile RTL")
except IOError as e:
raise TestException("Could not compile RTL: %s" % str(e))
output looks like this:
TestException: command 'sh' generated 2 errors, aborting.
When i run sh command seperately without python, i donot get any errors. I donot understand why python gives an exception
Try this instead:
/bin/sh compile_regression_vcs.sh
Is compile_regression_vcs.sh in the current directory?
The issue could mainly be the path for you shell and your shell script. It could also be that the environment that you are running in your shell maybe different from the one that you are using in your python script. Check this: extra_env=env
If you are still unable to zero in on issue you can debug further with following options:
You might want to use some debugging in your script to see the exact error. Using sys.exc_info() would be a good option to see stack details.
you could use this piece of code in your exception block:
import sys
tb = sys.exc_info()[2]
lst = format_list(extract_stack())
for l in lst: print l,
or you could just use:
import traceback
traceback.print_exc()
much cleaner :)
Refer to: traceback, sys
You can also run a python debugger to debug run_commands is executing as expected. Simply put these lines of code in your try block and you will get into the interactive python debugger mode.
try:
import pdb
pdb.set_trace()
self.agent.run_commands(cmd, pwd=self.agent.test_dir, extra_env=env)
you can later type help in python debugger mode to see all options available. Refer: pdb
You could also use inspect module: inspect module
精彩评论