yes, not a unix os but rather unix-like, and i want to program all of it on python, is that possible?? even the kernel, i want it all python. i know most kernels use c++ or c* but maybe python has a library to turn c* into python?? i’m still sort of a beginner but thanks and i would appreciate the answers
What OS will run the Python interpreter?
Can’t Python be translated into machine code and packaged into a binary? I swear I have no experience in OS development, just curious.
You’re absolutely right, you could take any binary that runs under an OS and set up a bootloader to execute it directly without an OS.
The problem is that all programs, even ones in C, rely invisibly and enormously on the OS abstracting away hardware for them. The python interpreter doesn’t know the first thing about how to parse the raw bytes on a hard drive to find the location of the bytes that belong to a given file path. Files and filesystems are ‘fake’ when you get down to it, and the OS creates that fiction so each program doesn’t have to be customized per PC setup.
So, ironically, to be able to truly kernel hack in python like you want would require writing tons of C to replace all OS hooks (like
fopen
to interact with a file, e.g.) with code that knows how to directly manipulate your hardware (speaking PCIe/NVMe to get to the disk, speaking GPT to find the partition on the disk, speaking ext4 to find the file in the partition, e.g.).OSes are complex as hell for a reason, and by retrofitting python to run on bare metal like that would require recreating that complexity in the interpreter.
Like Java, you can distribute a binary which bundles an interpreter/VM, but your code is still running inside a host OS.
Yes, and that’s basically what the CPython interpreter does when you call a Python script. It sometimes even leaves the result laying in your filesystem, with the extension .pyc . This is the byte code (aka machine code) for CPython’s implementation of the Python Virtual Machine (PVM).
Almost. The .pyc file is meant to run with the appropriate PVM, not for x86 or ARM64, for example. But if you did copy that .pyc to another computer that has a CPython PVM, then you can run that byte code and the Python code should work.
To create an actual x86 or ARM64 binary, you might use a Python compiler like cython, which compiles to x86 or ARM64 by first translating to C, and then compiling that. The result is a very inefficient and slow binary, but it is functional. You probably shouldn’t do this though.
This is incorrect; the term “machine code” refers to code that can be run on a real machine, not to code that requires a virtual machine.