1

I'm trying to call a Python module but get the following error "test.sh not found"

but this file is found in the directory.

process = subprocess.Popen("test.sh",shell=True)

The script and the sh file are located in the same directory.

3
  • 2
    Try sh test.sh or bash test.sh or even xterm test.sh. Commented Apr 28, 2014 at 20:48
  • do you also call the python script from the same directory? Commented Apr 28, 2014 at 20:50
  • @Torxed..thanks..it worked Commented Apr 28, 2014 at 20:52

1 Answer 1

3

By default the current directory is not in PATH therefore just "test.sh" is not found, the minimal change is to use "./tests.sh".

To run a shell script, make sure you have a valid shebang e.g., #!/bin/sh and the file has executable permissions (chmod u+x test.sh).

If you are running Python script from a different directory then you also need to provide the full path:

#!/usr/bin/env python
import os
import sys
from subprocess import check_call

script_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
check_call(os.path.join(script_dir, "test.sh"))

Note: there is no shell=True that starts the additional unnecessary shell process here. realpath resolve symlinks, you could use abspath instead if you want the path relative script's symlink instead of the script file itself.

Sign up to request clarification or add additional context in comments.

Comments

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.