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
Shell Script: Execute a python program from within a shell script - Stack Overflow
How to run a script from python shell? I can import it, but can run it :)
How can I call a shell script from Python code? - Stack Overflow
How do I learn python for bash scripting.
Can I execute shell commands directly from Python scripts?
How can I optimize Python shell scripts for performance?
How does Python compare to traditional shell scripting languages like Bash?
Videos
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
Title, i can type in python shell
import scripts.myhelloworldscript
and script runs, but i don't think it's right way to do it? (cause if i type same again script is not running again, i have to close and reopen the shell, and type again to make it run 2nd time...)
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.