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
    
This is a rather old question but the best/most complete answer is the one by João Víctor. Be sure to read down to it. –  Okuma.Tony Aug 13 '14 at 15:10

5 Answers 5

up vote 31 down vote accepted

Just make sure 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

Method 1 - Create a shell script:

Suppose you have a python file hello.py Create a file called job.sh that contains

#!/bin/bash
python hello.py

mark it executable using

$ chmod +x job.sh

then run it

$ ./job.sh

Method 2 (BETTER) - Make the python itself run from shell:

Modify your script hello.py and add this as the first line

#!/usr/bin/env python

mark it executable using

$ chmod +x hello.py

then run it

$ ./hello.py
share|improve this answer
3  
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
2  
#!/usr/bin/env python is what I was looking for ~ thanks! –  yoshi Jul 24 '14 at 20:39
    
I always forget #!/usr/bin/env python. Remember to make sure you're referencing the right python version + path for your system! –  Okuma.Tony Aug 12 '14 at 15:05

Enter the python script directly from the native shell

#!/usr/bin/python

print "hello"
share|improve this answer
    
That's what I tried first and brought me here. See João's answer - it's #!/usr/bin/env python –  yoshi Jul 24 '14 at 20:40

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

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

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.