0

Let's say I want to use a shell script to run a python file, called test.py, which is in a directory called Test in my home directory. I tried the following code, which does not work:

 #!/bin/bash
 echo "Starting."
 module load gcc/4.8.2
 module load python/3.4.1
 echo "Modules loaded."
 $HOME/Test/test.py
 exit 0

I do not believe that how I am trying to run the program ($HOME/Test/test.py) works. I have not been able to determine how to do this, despite searching for a long time. Any help would be appreciated.

Dan Lowe
57.6k20 gold badges133 silver badges115 bronze badges
asked Jul 18, 2015 at 2:47

1 Answer 1

1

This is likely one of the following, or it may be a combination of both.

The python script is not executable. Fix with:

chmod u+x $HOME/Test/test.py

The script does not start with a #! line pointing to python. Fix that by making this the first line of test.py:

#!/usr/bin/env python

You can also use a full path instead of using /usr/bin/env to use $PATH to resolve the name.

#!/usr/local/bin/python
answered Jul 18, 2015 at 2:58
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, after adding "chmod u+x $HOME/Test/test.py" to my program, it created an output file, but it did not include the output of test.py (which simply prints "Hello World"). Am I missing something else? I am using python 3.4.1, so do I need to write "#!/usr/bin/env python3" instead?
/usr/bin/env will just resolve whatever is after it to some full path, so if you use python as python3, then yes you would do that. You can also use a full path to the interpreter instead, such as #!/usr/bin/python3.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.