0% found this document useful (0 votes)
80 views18 pages

Executables With Python

The document compares compilers and interpreters in Python. A compiler analyzes and processes the entire program at once, resulting in machine-specific executable code that runs quickly. An interpreter analyzes and processes statements one at a time, resulting in intermediate bytecode that is executed more slowly by the interpreter. The CPython interpreter compiles Python source code to bytecode, then executes that bytecode via a virtual machine that can directly communicate with the CPU.

Uploaded by

Sadalbari Stern
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views18 pages

Executables With Python

The document compares compilers and interpreters in Python. A compiler analyzes and processes the entire program at once, resulting in machine-specific executable code that runs quickly. An interpreter analyzes and processes statements one at a time, resulting in intermediate bytecode that is executed more slowly by the interpreter. The CPython interpreter compiles Python source code to bytecode, then executes that bytecode via a virtual machine that can directly communicate with the CPU.

Uploaded by

Sadalbari Stern
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

EXECUTABLES WITH PYTHON

EXECUTABLES WITH PYTHON - COMPILER VS INTERPRETER

high-level

SOURCE CODE SOURCE CODE
language
print(„hello world“) print(„hello world“)

parses (reads) parses statement


whole file by statement

COMPILER INTERPRETER

executes 

MACHINE CODE
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR

low-level
 MACHINE CODE binary


language instructions
SEQUENCE
01011001011100010
01000101010101101
01110010111000100 01011001011100010
10100101010101011 CPU
EXECUTABLES WITH PYTHON - COMPILER VS INTERPRETER

COMPILER INTERPRETER

- spends longer with analysing and 
 - spends less time in analysing and 

processing the program processing

- resulting code is an executable
 - resulting code is an intermediate



that is a machine-specific binary code

- hardware interprets and executes
 - resulting code is interpreted by 



resulting code another program

- program execution is fast - program execution is relatively slow


MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON - PYTHON IMPLEMENTATIONS

PYTHON INTERPRETERS (SELECTION)

- Cython (Standard Python interpreter, implemented in


the C language)

- Jython (written in Java)

- Skulpt (written in Javascript)

- PyPy
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON - PYTHON INTERPRETER

high-level

SOURCE CODE SOURCE CODE *.py file

language print(„hello world“)


print(„hello world“)
parses statement
by statement
parses (reads)
whole file COMPILER

COMPILER BYTE CODE PYTHON *.pyc file


INTERPRETER

VIRTUAL 

MACHINE
executes 

instructions 

MACHINE CODE
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR

low-level
 MACHINE CODE encoded in


language SEQUENCE
01011001011100010 binary
01000101010101101
01110010111000100 01011001011100010
10100101010101011 CPU
EXECUTABLES WITH PYTHON - PYTHON INTERPRETER

SOURCE CODE
print(„hello world“)

The CPython interpreter is a


COMPILER program written in the C language.

Many C source files were compiled


BYTE CODE PYTHON by the GCC compiler into binary
INTERPRETER files for the corresponding OS.

The interpreter can hence be


VIRTUAL 
 regarded as abstraction of the
MACHINE physical CPU and can directly
communicate with it because it
consists of directly executable
MACHINE CODE
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR

machine code.
SEQUENCE
01011001011100010
CPU
EXECUTABLES WITH PYTHON

Exercise:

Open https://interactivepython.org/runestone/static/CS152f17/
GeneralIntro/ThePythonProgrammingLanguage.html

Read the short text and make the little quiz below.
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON

Bytecode:
▸ Is an intermediate representation of your program.

▸ Is what the virtual machine sees of your program - it could described as the machine code
for your virtual machine.

▸ Is a series of instructions for stack operations.

▸ Is cached as .pyc file (for the CPython Interpreter) -> we can read the byte code with the
‚disassembler’ module.
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON

Machine Code:
▸ Is written in machine language instructions which can be executed directly by a CPU.

▸ Is strictly numerical.

▸ Is the lowest level representation of a compiled or assembled program.

▸ Is hardware dependent.
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON

Assembly:
▸ Uses mnemonic codes to refer to machine instructions.

▸ For instance, an instruction to add memory data to a register in a x86-family processor


might be

 

add eax,[ebx] 


in original Intel syntax, 

whereas this would be written 


addl (%ebx),%eax 


in the AT&T syntax used by the GNU Assembler. 
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON

Instruction Set:
▸ Every processor has its own instruction set.

Instruction Set Architecture:


▸ The instruction set architecture is an abstract model of a the physical internals of a
computer.

Instruction:
▸ Instructions are patterns of bits corresponding to different commands of the machine.

▸ Instructions cause the CPU to perform specific tasks, like loading, jumping to a memory
address, performing operations on the ALU, or the registers.

▸ Instructions are hardware dependent.

▸ Most instructions have one or more opcode fields, specifying the basic instruction type
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR

(jump, load, add etc.) and more fields for the addressing modes, actual value etc.
EXECUTABLES WITH PYTHON

Instruction types

▸ Data handling and memory operations


- Set a register to a fixed constant value.
- Copy data from a memory location to a register, or vice versa (a machine instruction is often called move; however,
the term is misleading). Used to store the contents of a register, result of a computation, or to retrieve stored data to
perform a computation on it later. Often called load and store operations.
- Read and write data from hardware devices.
▸ Arithmetic and logic operations
- Add, subtract, multiply, or divide the values of two registers, placing the result in a register, possibly setting one or
more condition codes in a status register.
- increment, decrement in some ISAs, saving operand fetch in trivial cases.
- Perform bitwise operations, e.g., taking the conjunction and disjunction of corresponding bits in a pair of registers,
taking the negation of each bit in a register.
- Compare two values in registers (for example, to see if one is less, or if they are equal).
- Floating-point instructions for arithmetic on floating-point numbers.
▸ Control flow operations
- Branch to another location in the program and execute instructions there.
- Conditionally branch to another location if a certain condition holds.
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR

- Indirectly branch to another location.


- Call another block of code, while saving the location of the next instruction as a point to return to.
▸ Coprocessor instructions
- Load/store data to and from a coprocessor, or exchanging with CPU registers.
- Perform coprocessor operations.
EXECUTABLES WITH PYTHON
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR

https://en.wikipedia.org/wiki/Instruction_set_architecture#Instructions
EXECUTABLES WITH PYTHON

The Fetch-Decode-Execute-Cycle:
▸ https://www.hartismere.com/20398/CPU-Fetch-Decode-Execute-Animation
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON - PYTHON BYTECODE

Python’s disassemble module makes it possible to view the actual byte code in a
human readable form
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON - PYTHON BYTECODE

INSTRUCTION

line offset argument



index

operation
 argument

name value
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON

Executables:
▸ Executables are programs that are developed for a specific platform or device.

▸ They have binary format and comply to the demands of the specific hardware architecture.
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR
EXECUTABLES WITH PYTHON

PYTHON LIBRARIES FOR BUILDING EXECUTABLES (SELECTION)


Cross platform (not mobile):
- cxFreeze (https://anthony-tuininga.github.io/cx_Freeze/)

- bbFreeze (https://pypi.org/project/bbfreeze/)

- PyInstaller (https://github.com/pyinstaller/pyinstaller/wiki/Supported-
Packages)

Windows only:
- Pynsist (https://pynsist.readthedocs.io/en/latest/)

- Py2exe (https://sourceforge.net/projects/py2exe/files/py2exe/)

Mac OS only:
MDM TECHNOLOGICAL BASICS II WS 2018/19 H.LINGOR

- Py2app (https://py2app.readthedocs.io/en/latest/)

iOS and Android:


- Buildozer (https://buildozer.readthedocs.io/en/latest/index.html)

You might also like