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
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
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
To all experts: Shell as a scripting language vs Python as a scripting language
How to include python script inside a bash script - Unix & Linux Stack Exchange
How can I call a shell script from Python code? - Stack Overflow
Shell scripting in Python
Videos
My take:
Python is probably a great language(what I saw and importantly, used for very little time), but that never enticed me to stick with it on a long-term basis.
See! The shortcoming and understanding. Some conventions in that language look and importantly, feel ridiculous.
My lacuna ...so I refrained from it as much as possible. That certainly does NOT demean Python's ability.
Shell is comparatively easy (take it with a pinch of salt :) ) to learn and use. And it certainly has some eye-popping drawbacks. But the world used to run on it and still some do.
What do you use more frequently?
PS: There is a catch, for the month-end paycheck you are force to use something, that you might not like. I am ignoring that "important fact" in this argument. Looking for pure technical merits.
Just pass a HereDoc to python -.
From python help python -h:
- : program read from stdin
#!/bin/bash
MYSTRING="Do something in bash"
echo $MYSTRING
python - << EOF
myPyString = "Do something on python"
print myPyString
EOF
echo "Back to bash"
You can use heredoc if you want to keep the source of both bash and python scripts together. For example, say the following are the contents of a file called pyinbash.sh:
#!/bin/bash
echo "Executing a bash statement"
export bashvar=100
cat << EOF > pyscript.py
#!/usr/bin/python
import subprocess
print 'Hello python'
subprocess.call(["echo","$bashvar"])
EOF
chmod 755 pyscript.py
./pyscript.py
Now running pyinbash.sh will yield:
$ chmod 755 pyinbash.sh
$ ./pyinbash.sh
Executing a bash statement
Hello python
100
The subprocess module will help you out.
Blatantly trivial example:
>>> import subprocess
>>> subprocess.call(['sh', './test.sh']) # Thanks @Jim Dennis for suggesting the []
0
Where test.sh is a simple shell script and 0 is its return value for this run.
There are some ways using os.popen() (deprecated) or the whole subprocess module, but this approach
import os
os.system(command)
is one of the easiest.