Shell Script: execute python program from shell script

I tried to find the answer, but no luck.

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

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

How can I do that?

+56
python shell
Dec 07 '10 at 13:30
source share
6 answers

Just make sure the python executable is in the PATH environment variable and then add to your script

 python path/to/the/python_script.py 

More details:

  • In job.sh file put this
 #!/bin/sh python python_script.py 
  • Run this command to execute the script for you: chmod u+x job.sh
  • Run it: ./job.sh
+84
Dec 07 '10 at 13:34
source share

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 with executable file using

 $ chmod +x job.sh 

then run it

 $ ./job.sh 

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

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

 #!/usr/bin/env python 

mark it with executable file using

 $ chmod +x hello.py 

then run it

 $ ./hello.py 
+63
Dec 07 '10 at 13:54
source share

IMHO, writing

 python /path/to/script.py 

Absolutely wrong, especially these days. Which python? python2.6? 2.7? 3.0? 3.1? In most cases, you need to specify the python version in the shebang tag of the python file. I recommend using

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

In this case, it is much better to have a script executable and call it directly:

 #! / bin / bash

 /path/to/script.py

Thus, you only need the python version in one file. Most systems currently have python2 and python3, and it happens that symlink python points to python3 , while most people expect it to point to python2 .

+4
Dec 07 '10 at 15:08
source share

you should be able to call 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 with chmod u + x scriptname.py

EDIT: beat: -p

+1
Dec 07 '10 at
source share

This works best for me: Add this at the top of the script:

 #!c:/Python27/python.exe 

(C: \ Python27 \ python.exe is the path to python.exe on my machine) Then run the script with:

 chmod +x script-name.py && script-name.py 
+1
Jul 21 '15 at 4:12
source share

This works for me:

  • Create job.sh and add a command to run the python script (you can even add command line arguments to this python, I usually predefine the command line arguments).

    chmod + x job.sh

  • inside job.sh add the following py files, say:

    python_file.py argument1 argument2 argument3 β†’ testpy-output.txt && & &&& echo "completed with python_file.py"

    python_file1.py argument1 argument2 argument3 β†’ testpy-output.txt && & &&& echo "completed with python_file1.py"

  • The result of job.sh should look like this: completed with python_file.py

  • I use this usually when I have to run multiple python files with different arguments predefined.

Note: Just quickly describe what happens here:

 python_file.py argument1 argument2 argument3 >> testpy-output.txt && echo "completed with python_file.py" . 

Here the shell script will run the python_file.py file and add some command line arguments at runtime to the python file. This does not necessarily mean that you must also pass command line arguments. You can just use it like: python python_file.py, simple and simple. Then β†’ prints and saves the output of this .py file to the testpy-output.txt file.

& is a logical operator that will be launched only after successful execution of the above and as an additional echo "completed with python_file.py" will be reflected to your client / terminal at runtime.

+1
Sep 12 '16 at 6:05
source share



All Articles