The files with a .py extension contain Python programming language code. In this way when you execute the file that sequence of code is executed.
Unlike a .sh file which executes instructions that any Linux system can execute, for a .py file to work you will have to install Python.
This is the first thing you have to do if you want to start learning to program with Python.
Install Python and check version on Ubuntu and Linux
In order to do this, you have to have a Python environment prepared. On Linux you can
It returns the version of python that we have installed and if we don't have any we can install it. We use version 3.x and we forget about 2.7.x which is obsolete. We are going to install Python 3 on Ubuntu
sudo apt install python3
It will ask us for the password of the super user and once installed we will check the version again to make sure everything is fine.
With this already you can run the files. Open the console and go to the folder where the .py is. Suppose we have a file hello-world.py in the Downloads folder
Now we give you permissions
chmod +x hello-world.py
And finally we execute it
Keep in mind that a .py file can make something visible, something internal that you don't see, or just be a module, that is, a file with Python functions, variables, etc. which is used internally to create scripts and programs.
Execute or read the file graphically
If you want to do it at the click of a button. In this article I explained how to do it. It is a way of leaving any extension configured so that when you double-click it, it asks you if you want to run it or if you prefer to open the file with the software you have chosen. It is explained for .sh but it is the same for any extension.
How to create a .py
I take this opportunity to explain how to create a .py file
To see the code you can use a text editor or an IDE, which would be ideal if you intend to learn to program or if you want to modify the code. Right now I use Gedit as an editor and text and as an IDE I use Visual Studio Code.
The quick way with the terminal is to use the touch command
The good thing about using an IDE like Visual Studio is that you can use the terminal in the same IDE, so that at the same time you create the file you can give it permissions, debug it, etc. All this without counting the amount of possibilities that they offer us.