Run python file in terminal with arguments

Python programs can be easily run on the IDEs and also you can run python files in the terminals.

You can run the python file in the terminal using python3 filename.py command.

In this tutorial, you’ll learn how to

  • Run python file in terminal
  • Run python file in terminal with arguments
  • Use Python3 Command Line Arguments

For demonstration, you’ll use the python script file sample.py available in your Ubuntu system.

  • Install Python on Ubuntu
  • Run Python File in Terminal
  • Run Python File in Terminal With Arguments
  • Using Python3 Command Line Arguments
  • Conclusion

Install Python on Ubuntu

Ubuntu 20.04 and other Debian Linux distributions are shipped with the Python 3 installed by default. You can confirm if python is available by using the below command.

python3 -V

You’ll see the python version installed as below.

Python 3.8.2

If it’s not installed, you can install Python3 on Ubuntu by using the following command.

sudo apt install python3.8

This will install Python 3.8 in Ubuntu and you can verify it again by using the command python3 -V as shown above.

If you want to install a different version of Python, you can update the version number which is highlighted in yellow.

Python is installed.

Now, you’ll see how to run python scripts in Linux.

In this section, you’ll learn how to run python files in the terminal.

Python files contain python scripts to be executed. So this section will be the answer to the question of how to run python scripts in Linux[Ububtu].

In this tutorial, you’ll use the interpreter along with the filename to execute the script.

Use the command python3 along with the python script file name as shown below.

python3 sample.py

Now the python3 will run the script available in the file and you’ll see the console output as you’ve written in your script.

Next, you’ll see how to run a python file in the terminal with arguments.

Run Python File in Terminal With Arguments

In this section, you’ll learn how to run a python file in the terminal with arguments from the command line.

You can pass the arguments to python from the Linux command line by specifying the argument’s value next to the commands. If you have two or more commands, you can separate them with the space.

Use the below command to pass var1, var2 to the python script file sample.py.

python3 sample.py var1 var2

Now, your program will accept this command-line argument and process it as defined in the script.

Using Python3 Command Line Arguments

In this section, you’ll learn how to use Python3 command-line arguments in your python script.

You can use the command line arguments by using the sys.argv[] array.

The first index of the array consists of the python script file name. And from the second position, you’ll have the command line arguments passed while running the python script.

Consider the below python script available in the file name called sample.py.

import sys
print sys.argv[0] 
print sys.argv[1] 
print sys.argv[2]

Run the script file using the below command.

python3 sample.py var1 var2

You’ll see the below output.

sample.py
var1
var2

This is how you can use the command line arguments inside your python script.

Conclusion

In this tutorial, you’ve learned how to run a python file in a terminal (Ubuntu or any other bash command line terminal). You’ve also learned how to run a python file in the terminal with arguments while running and also learned how to access those command-line arguments inside the python script.

how to create a python file in the terminal?

You can create a python file in the terminal using the command sudo vim filename.py

how run a python file in the terminal?

You can run the python file in terminal using the python3 command alone with the filename as python3 filename.py

I know that I can run a python script from my bash script using the following:

python python_script.py

But what about if I wanted to pass a variable / argument to my python script from my bash script. How can I do that?

Basically bash will work out a filename and then python will upload it, but I need to send the filename from bash to python when I call it.

asked Jan 4, 2013 at 10:48

2

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

Run python file in terminal with arguments

MisterMiyagi

39.1k9 gold badges88 silver badges102 bronze badges

answered Sep 23, 2015 at 22:07

Run python file in terminal with arguments

iamthedrakeiamthedrake

2,0691 gold badge17 silver badges13 bronze badges

3

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.

answered Jan 4, 2013 at 10:55

mikumiku

175k46 gold badges302 silver badges307 bronze badges

1

Use

python python_script.py filename

and in your Python script

import sys
print sys.argv[1]

answered Jan 4, 2013 at 10:50

NPENPE

470k103 gold badges921 silver badges994 bronze badges

0

Embedded option:

Wrap python code in a bash function.

#!/bin/bash

function current_datetime {
python - <

Use environment variables, to pass data into to your embedded python script.

#!/bin/bash

function line {
PYTHON_ARG="$1" python - <

http://bhfsteve.blogspot.se/2014/07/embedding-python-in-bash-scripts.html

answered Mar 21, 2015 at 16:56

user77115user77115

5,4356 gold badges35 silver badges40 bronze badges

1

use in the script:

echo $(python python_script.py arg1 arg2) > /dev/null

or

python python_script.py "string arg"  > /dev/null

The script will be executed without output.

answered Jul 27, 2013 at 15:31

Run python file in terminal with arguments

burseanerburseaner

8852 gold badges16 silver badges27 bronze badges

1

I have a bash script that calls a small python routine to display a message window. As I need to use killall to stop the python script I can't use the above method as it would then mean running killall python which could take out other python programmes so I use

pythonprog.py "$argument" & # The & returns control straight to the bash script so must be outside the backticks. The preview of this message is showing it without "`" either side of the command for some reason.

As long as the python script will run from the cli by name rather than python pythonprog.py this works within the script. If you need more than one argument just use a space between each one within the quotes.

answered Feb 16, 2017 at 19:28

0

and take a look at the getopt module. It works quite good for me!

answered Jan 4, 2013 at 11:07

HappyHackingHappyHacking

8781 gold badge12 silver badges28 bronze badges

1

Print all args without the filename:

for i in range(1, len(sys.argv)):
print(sys.argv[i])

answered Nov 18, 2020 at 10:49

Run python file in terminal with arguments

1

How do I run a Python file in terminal with arguments?

You can use the command line arguments by using the sys. argv[] array. The first index of the array consists of the python script file name. And from the second position, you'll have the command line arguments passed while running the python script.

How do I run a Python function in terminal?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do you give arguments in Python?

script name..
Example. Consider the following script test.py − #!/usr/bin/python import sys print 'Number of arguments:', len(sys. ... .
Parsing Command-Line Arguments. Python provided a getopt module that helps you parse command-line options and arguments. ... .
getopt. getopt method. ... .
Exception getopt. GetoptError..

How do I run a Python file on Mac terminal?

On a Mac system, it is very straight-forward. All you need to do is open Launchpad and search for Terminal , and in the terminal, type Python and boom, it will give you an output with the Python version.