Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've tried googling the answer but with no luck.

I need to use my works supercomputer server, but for my python script to run, it must be executed via a shell script.

For example I want job.sh to execute python_script.py

How can this be accomplished?

share|improve this question
    
Have you tried just putting python python_script.py in your shell script? –  thkala Dec 7 '10 at 13:33
    
Um, python python_script.py. Or just ./python_script.py if the script got a shebang. –  delnan Dec 7 '10 at 13:34
2  
@delnan: ./python_script.py also requires that the script is executable –  thkala Dec 7 '10 at 13:36
    
what exactly have you tried and what is the problem? Either you did not do that good of a google search, or there is a lot that you are not telling us... –  thkala Dec 7 '10 at 13:41
add comment

5 Answers

up vote 16 down vote accepted

Just be carefull the python executable is in your PATH environment variable then add in your script

python path/to/the/script.py

Details:

  • In the file job.sh, put this
#!/bin/sh
python python_script.py
  • Execute this command to make the script runnable for you : chmod u+x job.sh
  • Run it : ./job.sh
share|improve this answer
    
So simple thank you –  Harpal Dec 7 '10 at 13:47
add comment

Enter the python script directly from the native shell

#!/usr/bin/python

print "hello"
share|improve this answer
add comment

job.sh

#!/bin/bash
python code.py

$ chmod +x job.sh
$ ./job.sh

but you can run python codes like shell code

hello.py

#!/usr/bin/env python
print "hello world"

$ chmod +x hello.py
$ ./hello.py
share|improve this answer
2  
Please edit your answer to make it more readable. You can use the 101010 button in the answer editor to mark the script contents as code. –  thkala Dec 7 '10 at 14:16
add comment

Imho, writing

python /path/to/script.py

Is quite wrong, especially in these days. Which python? python2.6? 2.7? 3.0? 3.1? Most of times you need to specify the python version in shebang tag of python file. I encourage to use

#!/usr/bin/env python2 #or python2.6 or python3 or even python3.1
for compatibility.

In such case, is much better to have the script executable and invoke it directly:

#!/bin/bash

/path/to/script.py

This way the version of python you need is only written in one file. Most of system these days are having python2 and python3 in the meantime, and it happens that the symlink python points to python3, while most people expect it pointing to python2.

share|improve this answer
add comment

you should be able to invoke it as python scriptname.py

eg

# !/bin/bash

python /home/user/scriptname.py 

also make sure the script has permissions to run

you can make it executable by using chmod u+x scriptname.py

EDIT: beaten to it :-p

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.