Home » Python » Shell Script: Execute a python program from within a shell script

Shell Script: Execute a python program from within a shell script

Posted by: admin November 30, 2017 Leave a comment

Questions:

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?

Answers:

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

python path/to/the/python_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
Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

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 the python.exe on my machine)
Then run the script via:

chmod +x script-name.py && script-name.py

Questions:
Answers:

This works for me:

  1. Create a job.sh and add command to run python script (you can even add command line arguments to that python, i usually pre-define my command line arguments).

    chmod +x job.sh

  2. 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”

  3. Output of job.sh should look like this:
    completed with python_file.py
    completed with python_file1.py

  4. I use this usually when I have to run multiple python files with different arguments, pre defined.

Note: Just a quick heads up on what’s going on here:

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

Here shell script will run the file python_file.py and add multiple command-line arguments at run time to the python file. This does not necessarily means, you have to pass command line arguments as well. You can just use it like: python python_file.py, plain and simple. Next up, the >> will print and store the output of this .py file in the testpy-output.txt file.

&& is a logical operator that will run only after the above is executed successfully and as an optional echo “completed with python_file.py” will be echoed on to your cli/terminal at run time.