How to 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

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.

Run Python File in Terminal

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

2 thoughts on “How to Run Python File in Terminal [ With Arguments ]?”

  1. What I would like to know is how to run a python script in terminal after invoking REPL, dispensing with the need enter “python” before naming the script file.

    Reply

Leave a Comment