I'm looking to run a bash command that runs a python file ie: "python /Users/declanmcgranahan/airflow/dags/tasks/aircall_calls_to_csv.py"
. But I have a variable in that python file that I want to define in the bash command. Is this possible?
Asked
Active
Viewed 66 times
3 Answers
2
Do you mean something like python test.py var1 var2
?
Take a look at this link: Python - Command Line Arguments
This even provides options function like python test.py -a var1 -b var2

Zachary Liu
- 75
- 6
-
exactly what I needed. Got it up and running. Thanks. – declac Jun 06 '19 at 23:46
-
@declac : If you really think about having a **variable** defined in the bash world and read from Python, you could use an environment variable. – user1934428 Jun 07 '19 at 07:14
0
You can pass arguments to the bash command and then read in those arguments into required variables in the __main__
handler function. Look up argparse library on how to use it to handle provided arguments. The code in your python file should have a block:
if __name__ == "__main__":
# use argparse to assign variables

Anuj Kumar
- 130
- 1
- 9
0
you could pass a parameter to the python script on the command line, or you could set an environment variable in the script and have the script read it using getenv
.
Command-line arguments: How to read/process command line arguments?

jspcal
- 50,847
- 7
- 72
- 76