DEV Community

Cover image for How to convert a Python file to an ELF file, and how to execute the generated ELF file with/without using the terminal in Linux?
Prathima Bommannagari
Prathima Bommannagari

Posted on

How to convert a Python file to an ELF file, and how to execute the generated ELF file with/without using the terminal in Linux?

What is an ELF file?

An ELF(Executable and Linkable Format) file is an executable standard file format for the Linux and Unix platforms.

The ELF file format is typically used by binary executables in Linux. These serve as Linux's equivalent to Windows' PE (Portable Executable) format, which uses the EXE file extension.

In addition to the executable code, executable files also include data such as static and dynamic data, linker information, and library imports.


How to convert a Python file to an ELF file?

To generate an ELF file from a python file, we can use pyinstaller.

  1. Install pyinstaller using the below command:
    pyinstaller commandIf you are using a python version that's older than python3, replace pip3 with pip in the above command.

  2. Create a .py file or go to the path where your existing python file resides.
    Python file

  3. Run the below command to convert the python file to an executable:
    ELF conversion

  4. Pyinstaller creates a dist(distribution) directory, that contains the main executable(ELF) and the dynamic libraries bundled in an executable file.
    elf file generated


How to run the executable file in Linux?

Using the terminal:

  1. Open the terminal.
  2. Go to the path where the ELF file is located and execute the ELF file by placing ./ before the elf filename as below: Running the ELF file using terminal

Without using the terminal:

  1. Open the File explorer and go to the path where the ELF is located.
  2. Right-click on the executable file, go to Properties--> Permissions tab--> check the box 'Allow executing file as program' --> Close the properties window. Running ELF without using the terminal3. Run the executable file by just double-clicking on it (similar to running an exe file on Windows).

Top comments (1)

Collapse
 
pbhagat profile image
Paras Bhagat

Very nicely explained with such simplified details..Good work 👍