To execute a python script in a bash script you need to call the same command that you would within a terminal. For instance
> python python_script.py var1 var2
To access these variables within python you will need
import sys
print(sys.argv[0]) # prints python_script.py
print(sys.argv[1]) # prints var1
print(sys.argv[2]) # prints var2
Answer from iamthedrake on Stack OverflowTo execute a python script in a bash script you need to call the same command that you would within a terminal. For instance
> python python_script.py var1 var2
To access these variables within python you will need
import sys
print(sys.argv[0]) # prints python_script.py
print(sys.argv[1]) # prints var1
print(sys.argv[2]) # prints var2
Beside sys.argv, also take a look at the argparse module, which helps define options and arguments for scripts.
The argparse module makes it easy to write user-friendly command-line interfaces.
How to run a Python script on Linux? - Stack Overflow
How to run a python file taken as a parameter in a shell script? - Unix & Linux Stack Exchange
Shell Script: Execute a python program from within a shell script - Stack Overflow
call shell script with arguments in python
Can I run Python scripts within a virtual environment?
How can I avoid installing Python libraries globally?
What are virtual environments in Python?
Videos
You can also use the sys module. Here is an example :
import sys
first_arg = sys.argv[1]
second_arg = sys.argv[2]
def greetings(word1=first_arg, word2=second_arg):
print("{} {}".format(word1, word2))
if __name__ == "__main__":
greetings()
greetings("Bonjour", "monde")
It has the behavior your are looking for :
$ python parse_args.py Hello world
Hello world
Bonjour monde
Python provides more than one way to parse arguments. The best choice is using the argparse module, which has many features you can use.
So you have to parse arguments in your code and try to catch and fetch the arguments inside your code.
You can't just pass arguments through terminal without parsing them from your code.
Method one: some as Windows (not the best option)
Tell the file-manager to open files ending in .py in python /usr/bin/python3 (note the slash at the start).
Then double click it.
Method two (not the best option)
Open a shell and type python3 «script name»
Method three (the preferred method)
- Add
#!/usr/bin/python3as the first line of the script. - Make the file executable
chmod +x «script name»(or right click in your file-manager).
side note:
With this method it is better to remove the .py, as it is not needed, and leaks the implementation: All callers of the script need to know the programming language.
first get your desktop mint with this command :
cd /home/<USER>/Desktop
after enter just enter command:
python3 myscript.py
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
I am trying to call a bash script via Popen but I am getting an error. bufsize must be an integer.
bash_args = '-a test -b 24 -c 15'
subprocess.Popen('/home/pi/bash.testsh', bash_args)The script does work. If I run via command line I get expected output, but via python I am getting errors.
Actually, wouldn't we want to do this?
import sys
sys.argv = ['abc.py','arg1', 'arg2']
execfile('abc.py')
try this:
import sys
sys.argv = ['arg1', 'arg2']
execfile('abc.py')
Note that when abc.py finishes, control will be returned to the calling program. Note too that abc.py can call quit() if indeed finished.