Rechercher

2.5. Interpreting code

download PDF

This section shows how to byte-compile a program written in Python and raw-interpret a program written in bash.

Note

In the two examples below, the #! line at the top of the file is known as a shebang, and is not part of the programming language source code.

The shebang enables using a text file as an executable: the system program loader parses the line containing the shebang to get a path to the binary executable, which is then used as the programming language interpreter. The functionality requires the text file to be marked as executable.

2.5.1. Byte-compiling code

This section shows how to compile the pello.py program written in Python into byte code, which is then executed by the Python language virtual machine.

Python source code can also be raw-interpreted, but the byte-compiled version is faster. Hence, RPM Packagers prefer to package the byte-compiled version for distribution to end users.

pello.py

#!/usr/bin/python3

print("Hello World")

Procedure for byte-compiling programs varies depending on the following factors:

  • Programming language
  • Language’s virtual machine
  • Tools and processes used with that language
Note

Python is often byte-compiled, but not in the way described here. The following procedure aims not to conform to the community standards, but to be simple. For real-world Python guidelines, see Software Packaging and Distribution.

Use this procedure to compile pello.py into byte code:

Procédure

  1. Byte-compile the pello.py file:

    $ python -m compileall pello.py
    
    $ file pello.pyc
    pello.pyc: python 2.7 byte-compiled
  2. Execute the byte code in pello.pyc:

    $ python pello.pyc
    Hello World

2.5.2. Raw-interpreting code

This section shows how to raw-interpret the bello program written in the bash shell built-in language.

bello

#!/bin/bash

printf "Hello World\n"

Programs written in shell scripting languages, like bash, are raw-interpreted.

Procédure

  • Make the file with source code executable and run it:

    $ chmod +x bello
    $ ./bello
    Hello World
Red Hat logoGithubRedditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez leBlog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

© 2024 Red Hat, Inc.