"Compiling" Python

Ξεκίνησε από mandarinos, 09 Ιαν 2025, 09:58:52 ΠΜ

« προηγούμενο - επόμενο »

mandarinos

Καλή χρονιά!

Επειδή η Python μπήκε στο σχολικό πρόγραμμα, αναρωτήθηκα πολλές φορές, αν υπάρχει compiler γιά Python - και δή, free / open source. (...Αλλοιώς, τί να την κάνουμε απλά μιά γλώσσα προγραμματισμού παραπάνω;... και δή, παραθυρική "με το ζόρι"; Ή, τί να πω στους μαθητές μου, προσπαθώντας να εξηγήσω το γιατί τόσο πρόσφατο "hype" με την Python;)

Δεν βρήκα άκρη ψάχνοντας στο Διαδίκτυο, έως ότου ρώτησα την Τεχνητή Νοημοσύνη ChatGPT. (Η οποία, μάλιστα, απάντησε ότι οι απαντήσεις της δεν είναι copyrighted, και είναι ελεύθερες ακόμη και γιά εμπορική χρήση.)

Η απάντησή της ακολουθεί (copy/paste), και πιστεύω θα βοηθήσει αρκετούς συναδέλφους και μαθητές:

......................................

Ερώτηση:

Is there a Python compiler?


Απάντηση ChatGPT:

Python does not have a traditional compiler in the same sense as languages like C or Java, but there are tools and methods to "compile" Python code. Here's an overview:


1. Python Bytecode Compilation
Python code is typically interpreted by the CPython interpreter. However, Python does have a compilation step:
  • Python code (.py files) is compiled into bytecode (.pyc files) by the Python interpreter.
  • This bytecode is executed by the Python Virtual Machine (PVM).
This process happens automatically, and the .pyc files are stored in a __pycache__ directory.

2. Compiling Python to Executables
If you want to create standalone executable files, there are tools to "freeze" Python code into binaries:
  • PyInstaller:
      Converts Python scripts into standalone executables. Example:
                           pip install pyinstaller
                           pyinstaller --onefile script.py
This creates an executable file (script) that can run without requiring Python to be installed on the target machine.
  • cx_Freeze:
    Similar to PyInstaller, used to bundle Python applications into executables.
  • Nuitka:
    A Python-to-C compiler that converts Python code into native machine code for better performance. Example:
                           pip install nuitka
                           nuitka --standalone script.py


3. Compiling Python to Other Languages
Some tools translate Python code into other languages for performance optimization or platform compatibility:

  • Cython:
      Translates Python code into C code for improved performance and then compiles it into a shared library or executable. Example:
                           pip install cython
                           cython --embed -o script.c script.py
                           gcc -o script script.c $(python3-config --cflags –ldflags)

  • Shedskin:
      Converts a subset of Python into optimized C++.

4. JIT Compilation
For real-time performance improvements, Python has Just-In-Time (JIT) compilers:
  • PyPy:
      An alternative Python interpreter with a built-in JIT compiler that improves runtime performance. Example:
                           sudo apt install pypy3
                           pypy3 script.py


Summary
  • Python does not have a traditional compiler for producing machine code directly, as it's primarily an interpreted language.
  • However, tools like PyInstaller, Cython, Nuitka, and PyPy offer ways to compile Python code into executables, C extensions, or faster bytecode.
Let me know which approach you'd like to explore!


mandarinos

Γενικά μιλώντας, έχω κάνει κι άλλες ερωτήσεις σε ΤΝ γιά εξειδικευμένα θέματα προγραμματισμού. (Δηλαδή, πχ πώς καλώ εντολές του Linux μέσα από μιά γλώσσα, κλπ.) Οι απαντήσεις τους, όμως, έχουν μιά ακρίβεια κάπου 90%-95%, έχετέ το υπ' όψιν σας. Το υπόλοιπο τους διαφεύγει, παρεκτός αν διευκρινήσετε ποιά έκδοση της γλώσσας προγραμματισμού χρησιμοποιείτε. Τότε, μπορούν να φτάσουν και το 99%-100%.

Λιγάκι πιό ακριβές είναι το Grok, αλλά θέλει να είσαι χρήστης του X (πρώην Twitter).

falexakos

Παράθεση από: mandarinos στις 09 Ιαν 2025, 10:07:21 ΠΜπχ πώς καλώ εντολές του Linux μέσα από μιά γλώσσα,
Για το συγκεκριμένο (μιας και ρωτάς) υπάρχει η κλήση system() στην C. Π.χ. system("ls -l *.c");
Περισσότερα εδώ: https://www.tutorialspoint.com/c_standard_library/c_function_system.htm


"Be ashamed to die until you have won some victory for humanity."
Horace Mann (USA, 1796–1859)

mandarinos

#3
Σαν παράδειγμα το έφερα. (Αυτά είναι τα εύκολα! :) ) Την ΤΝ τη ρωτάω, όταν βαρυέμαι το ψάξιμο / όταν δεν βρίσκω στα βιβλία, ή στις διαδικτυακές αναφορές αυτό ακριβώς που θέλω / όταν δεν βγάζω άκρη με τον πειραματισμό με τον κώδικα / όταν δεν θέλω να ενοχλήσω (ακόμη) τα forums των προγραμματιστών.

Πληροφοριακώς, χρησιμοποιώ Gambas3. Συγκριτικά με οποιαδήποτε άλλη γλώσσα, την θεωρώ *ΤΟ* εργαλείο.

- Η C καλή είναι, αλλά θέλει οπωσδήποτε παραθυρικό συμπλήρωμα. (Με καμιά Rust ίσως.)
- Η Python, τα είπαμε.
- Η Java... γιά να γράψεις μιά "καλημέρα" στην οθόνη, πρέπει πρώτα να κάνεις ένα τρίμηνο θεωρία τί είναι "main", τί είναι "void()", αν πρέπει το πρόγραμμά σου να ονομάζεται το ίδιο με την κύρια ρουτίνα σου, κλπ. Συν τις αναντιστοιχίες των νέων εκδόσεων με την υπάρχουσα γραπτή τεκμηρίωση.