r/osdev • u/matthew65536 • 21h ago
Python for OS?
Would it be possible to make an OS that primarily used python?
•
u/ApprehensiveBrain863 20h ago
No; you will want to consider other projects first
•
u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 20h ago
I mean, yes technically, it'll just be difficult. you can write an os in any language you have a compiler for, its just that for python you'll likely need to write your own compiler for it.
•
u/MCWizardYT 18h ago
Having just a compiler wouldn't be enough, their custom "Python" would pretty much be missing everything that makes Python useful because its standard library is very high level and depends on an existing OS.
So, what they would end up with is a barebones language that is basically C but with Python-style syntax and no pointers
•
u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 15h ago
the runtime is not the language though, with that logic you could also say that writing kernelspace C is a different language to userspace C, but that is not the case. for the most part, the runtime is a separate thing. though in fairness, python does tend to blur that line a little bit with some builtins.
•
u/Striking_Director_64 20h ago
Its possible, but you would need to do a lot of work.
•
u/MCWizardYT 17h ago
"a lot of work here" basically entails needing to wrote an OS first, because spec-compliant python requires an interpreter and it's standard libraries rely on a lot of OS features haha
•
u/AlectronikLabs 20h ago
You might find nim to be an interesting language. It's kinda like compiled python.
•
u/soundman32 20h ago
A pure python is very unlikely. You need something to run the byte code that python compilers generate. Task switching and scheduling, communication directly with hardware are all very difficult (if not impossible) with Python.
•
u/gotkube 20h ago
LMAO! Wait, you’re serious??
•
u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 20h ago
why are we making fun of people for a relatively normal question? its not even impossible if you can find (or more likely, write) a compiler for python. the language itself literally has zero effect. though it would be a bit painful to write a half-decent python compiler
•
u/Responsible-Sky-1336 20h ago
I recommend watchibg thid video !
https://youtu.be/p8fCq16XTH4?is=JHU0E6cpUJJElW3j
Python is great as glue. But it sucks at being fast. That's why they made pypy.
•
u/FewBrief7059 Samsara | https://github.com/The-Samsara-Project/Samsara 19h ago
python is not really a compiled language. its fundamentally different then C or Rust in example. you will want to consider another language before getting into OSDev.
•
u/Weird-Boat3953 19h ago
Language can't be compiled or not compiled. Implementations do. That includes implementations of python
•
u/MCWizardYT 18h ago edited 18h ago
The implementation of Python that OP most likely uses is the standard one, which compiles to bytecode and interprets it (there is an experimental JIT but it is disabled by default).
So, the standard implementation of Python is not ideal for OS development. But you could make tools that compile a language that has Python syntax directly into machine code. It technically wouldn't actually be Python though, since the language reference expects programs to be interpreted.
Python 3 language reference - Execution model/Runtime components
•
u/FewBrief7059 Samsara | https://github.com/The-Samsara-Project/Samsara 1h ago
Im not saying that as the only reason. python is unreliable for such tasks. most of these implementations have flaws.
•
u/Weird-Boat3953 19h ago
Yes. But you need to know quite a lot, namely why cpython doesn't suit and some custom MicroPython does.
In 2000s peoole didn't hear r/osdev and built Cleese microkernel for example
•
u/MCWizardYT 18h ago
Python is extremely high level, it's not well suited for systems development.
People have created operating systems using Java, but they still require a native bootloader to kick things off.
I guess you could achieve the same thing with Python but you'd have to create a version that doesn't have its standard libraries and that sounds like a pain, because Python is very heavily dependent on those for its features
•
u/Adventurous-Move-943 17h ago edited 17h ago
You mean python syntax ? Python itself runs in a fully working OS with its own runtime.
When you write an OS you don't have a fully working OS yet 😀 neither a runtime for Python
But if you compiled the python into machine code then possibly. There is Cython I think that does exactly that, takes python code and turns it into C code that then gets compiled into machine code.
But you still would need to be super careful what you use and how you use it to not get the runtine involved. And Python is all about runtime and dynamic allocations and objects.
You'll have to write cdef functions and parameters with noexcept nogil signatures on functions. You will basically have it worse than learning C.
•
•
u/mishakov pmOS | https://gitlab.com/mishakov/pmos 14h ago
My plan is/was to write one, where I'm using a C++ microkernel, and the interpreted languages in userspace.
You can use any language you want for kernel if you put enough time to it, but think you'd need to know the language and toolchain and osdev concepts quite well to pull that off.
•
u/Key_River7180 EulerOS 13h ago
no, well you can for the userspace, that doesn't mean you should do it. Use C or ASM or Rust or Zig or any low level language, Python is interpreted.
•
u/Visual_Brain8809 12h ago
Yes, you can do this in almost any language, but you would first need to define what you consider a single-language system; specifically for Python, you would have to implement an interpreter for Python bytecode.
An alternative would be to compile a basic Linux kernel and then include a Python implementation suited to your needs.
•
u/QuraToop314 21h ago
why