Python is a popular and useful Programming Language mainly used for building Software Applications. Apart from Software Applications, Python Programming can be used to carry out automated tasks in an Operating system, it can be used to build Web Applications as well. With Python, specific tasks can be performed by creating custom scripts.
This article discusses how you can run a Python Script on Ubuntu 22.04.
How do I Run a Python Script on Ubuntu 22.04?
Most of the utilities and programs in Linux are written in Python and for this reason, Python Programming is a preferable way for task automation and running scripts in Ubuntu. Before running Python Scripts in Ubuntu, ensure Python is installed. Install Python by following the Install Python on Ubuntu 22.04 Guide if any previous version is not added or installed in Ubuntu.
Method 1: Using the python3 Command
The latest Python Version is “python3”. A Python Script in Ubuntu can be easily run in the Terminal directly with the “python3” command:
In our case, we have the “pythonScript.sh” Script File in the Home Directory:
To run the “pythonScript.sh” using the Terminal, the “python3” command will be executed as follows:
The Python Script will be executed and the Output can be seen in the Terminal. In our case, the “pythonScript.sh” Python Script prints the string “LinuxWays!”, and can be seen on the Terminal:
Similarly, any other Python Script File can be run with the “python3” Command in the Ubuntu Terminal.
Method 2: Using the “chmod” Command
The “chmod” command is a useful way of changing the file permissions. It can also execute a file, i.e. a Python Script File. To run a Python Script using the “chmod” command, you have to make a file with the “.sh” extension. Once the file is created, the “chmod” command can make the “.sh” file executable. In our case, we made the “script.sh” file with the “touch” command:
The “script.sh” file is created in the same Directory:
Open the newly created “script.sh” file with the “nano” command:
The Text editor for the “script.sh” file will open:
Add the following Python Script in the “script.sh” File:
print("Welcome to LinuxWays!")
The “#! /usr/bin/env” selects the python version to use.
Once you have added the script to the text editor, press the “ctrl+o” keys to write the script and then press the “ctrl+x” keys to close the text editor. Now, Before Running the Script File, use the “chmod” command which ensures to make the file executable.
In our case, we have the “script.sh” file, so the “chmod” command will be:
The “+x” makes the “script.sh” file executable. The cursor moves to the next line meaning the file can now be executed:
The executable file can be run directly in the Terminal with the following command:
In our case, the file name is “script.sh”. To run the Python Script now, the command will be:
This will display the output of the Script File, in our case “script.sh”, in the Terminal:
To learn more about making files executable and “chmod”, study the chmod +X – Making Files Executable Guide.
Method 3: Running Python Script With the “python” Command
The “python” command can also be used to run a Python Script in Ubuntu. It executes the selected Script File and displays the output in the Terminal:
In our case, the file name is “script.sh”, as seen in the Home Directory:
To run the “script.sh” Script File, the “python” command will be executed as follows:
This will display the output of the Script File in the Terminal, in our case, the output “Welcome to LinuxWays!” is displayed:
Conclusion
Python Script in Ubuntu can be run by using either the “python3” Command, the “chmod” method, or by using the “python” Command in the Terminal. With the “chmod” Command, a file with a “.sh” extension is created first, and then the file permissions are changed to make the file executable. This article explained all the commands to run a Python Script on Ubuntu.