How to Run Python File in Terminal [ With Arguments ]?

Introduction

Python is a general purpose programming language used in web development, data science, and to create scripts to perform simple activities in your computer.

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

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.

Prerequisites

Before you start, you’ll need the following.

  • If you do not have servers in the cloud, Create an AWS EC2 Ubuntu server instance by following the guide How to launch an EC2 Instance.
  • [Important] – Update the packages list in the server which is upgrade-able using sudo apt update
  • [Important] – Upgrade the packages in the server to the latest versions using sudo apt upgrade

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.

Output

Python 3.8.2

If its 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 script in Linux.

Run Python File in Terminal

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

Python files contains python scripts to be executed. So this section will be the answer to question how to run python script 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 python file in terminal with arugments.

Run Python File in Terminal With Arguments

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

You can pass the arguments to python from the Linux command line by specifying the arguments value next to the commands. If you have two or more commands, you can separate it 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 arguments 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 lin arguements 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.

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 learnt how to run python file in terminal (Ubuntu or any other bash command line terminal). You’ve also learnt how to run python file in terminal with arguments while running and also learnt how to access those command line arguments inside the python script.

What Next?

<Watch this space for more updates on Blog>

FAQs

how to create a python file in terminal?

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

how run python file in terminal?

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

Leave a Comment

Share via
Copy link
Powered by Social Snap