How to run a Python (.py) file on Linux

Python file on Linux

In the lists of the most popular programming languages ​​that are published every year, there is always a place for Python. The reasons can be many and varied, such as its versatility or that there is a lot of documentation throughout the network, but the fact is that it is one of the options to take into account. With so much fame, it is likely that sometimes they send us a python file and we don't know how to open it in linux.

In theory, opening a Python file in Linux is a simple task, but we can find one that does not open. If it's written from Windows, double-clicking the .py file might not open it even though it's marked to run as a program. This can happen because writing the code from Windows is not the same as from Linux; They are not "coded" the same, but don't worry because here we are going to explain how to execute them regardless of how they were created.

Run a python file on linux from the terminal

First of all, let's talk about something. Linux and Windows have a different API structure, and what is built from one operating system might not work on another. It's something that shouldn't happen, but it does. If something has been written to, for example, Visual Studio Code On Windows, we can go crazy trying to run the Python file on Linux, but the solution may be the simplest in the world: just "recompile" the file from Linux, which can be as simple as copy all the code, paste it in another file and save it with .py extension from our Linux-based operating system. With this problem solved, we continue.

In Linux, what never fails is to do things with the terminal. To avoid a potential danger, it is worth taking a look at the code, for which it will suffice to open it with a word processor such as gnome text editor or Kate. For example, and assuming that we do not understand much programming, we can search for "rm" to make sure that it is not going to delete anything from our hard drive without our permission. When we are sure that it is not dangerous, or if we trust the person who gave it to us, we can open the terminal and type "python" (without the quotes) followed by the name of the file including the path. To make no mistake, it's worth typing "python" (or "python3") and dragging the file to the terminal.

The good thing about doing it this way is that, if some exception (error) is thrown, we will see it in the terminal, and we can either fix it or give feedback to the developer. The bad thing is that, if it is an application with a graphical interface, we will see two windows, the terminal window in the background and the GUI of the app in the foreground.

The Shebang and execution permits

If what we have is something that will work only with the terminal, all this is not necessary. It is enough to do what was explained in the previous point. The problem can come when what we want is to run an application with a user interface. For this type of application to open in Linux, what we have to do is go to the top, to line 1, and put what is known as shebang (#!) accompanied by the path to python3. The complete string would be #!/usr/bin/env python3, although sometimes it also works or it is necessary to remove the "env" part and leave "python3" there. Basically, that line is saying which program should be in charge of executing the file.

But this will not be enough. We also need to give the Python file execute permission, which we'll accomplish by typing chmod +x ruta-al-archivo/ or with a right click and checking the box in file managers that support it.

With the permissions, the Shebang, and remembering and fixing the build of other operating systems if necessary, the .py gui file (which we can also find as .pyw) should open with a double click. If not, you can always try from the terminal (or from explorer/run from terminal). Of course, be careful what we open.

The content of the article adheres to our principles of editorial ethics. To report an error click here.

Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *



  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.