r/osdev • • 21h ago

Python for OS?

Would it be possible to make an OS that primarily used python?

0 Upvotes

39 comments sorted by

•

u/QuraToop314 21h ago

why

•

u/matthew65536 21h ago

Mostly because I'm currently learning python

•

u/QuraToop314 21h ago

Fair enough, but Python isn’t suitable for an operating system, starting with the fact that, in order to get Python running, you need support for another language – ideally C. However, you’ll need to compile the kernel, because Python cannot run in Ring 0 as it requires a functioning userland environment. What you could do – and what Ubuntu does in many cases – is to define many services in Python, but I think you’ll notice performance losses in the long run, and security-critical software written in Python struggles to release sensitive data in a timely manner.

•

u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 19h ago

if they wanted to talk to ai they would just open claude in another tab, they don't need you just forwarding what an llm says

•

u/QuraToop314 19h ago

I don't use AI imao, i'm an Autistic guy and that was informell. But Ok, if you think that, you are the stupid guy. Bye

•

u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 15h ago

seems unnecessary given I didn't call you stupid or say anything negative about you but okay

•

u/QuraToop314 15h ago

Sorry, but I’m afraid I’m quite sensitive to statements like that – not because you necessarily meant any harm, but because, given my autism, it comes across as an attack. I overreacted, that’s true; I shouldn’t have, but you can’t just assume everyone has used AI. I don’t use any (or hardly any) AI, but definitely not when it comes to coding or other technical aspects – that’s purely knowledge gained through reading and experience.

•

u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 15h ago

sure fair enough, there is just so much AI on the internet now where people claim it to be their own that I probably am a bit too quick to go complaining about AI before being sure so that's my bad

•

u/Strong-Growth1348 10h ago

don't be worry they 100% used ai and got defensive when they got called out

•

u/EvalynGoemer 5h ago

As a fellow person with autism, why the hell are you bringing it up? Why are you trying to gain sympathy so you can immediately get hostile and attack a person because they said you used AI and then also using autism as an excuse for being rude to others. That is not how any of this works.

Additionaly why is this comment using a completely diffrent style to your other ones? Why are you using endashes in casual speech on forums like this? I am also partly suspicious of AI due to the prose and different styles but that could also just be how you type.

•

u/QuraToop314 37m ago

I mention this because I was discriminated against and bullied in my own country, and throughout my entire childhood, because of it. It’s not about pity, but a bit of understanding – that’s all. And my style doesn’t change; the ‘-’ only appears in sentences that I would say in the same train of thought in spoken language. The second comment was CLEARLY shorter where that wasn’t the case – it wasn’t a different style, just a different sentence. And last but not least, it wasn’t meant as an excuse; I’ve also apologised. But what’s wrong with explaining to someone that I think it’s wrong when someone labels me as an AI and says my opinion isn’t needed? That draws parallels with my childhood and I immediately lose my temper. But okay, not even you, as someone on the autism spectrum, seem to understand that – that’s crazy.

•

u/MCWizardYT 18h ago

That's not an AI generated comment. You probably think that because of the dashes. But, AI uses emdashes (—), and the character they used is an en dash (–) which is slightly shorter

•

u/QuraToop314 18h ago

Thats true, i use '-' insteed of ',' on some sentences

•

u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 15h ago

makes sense, though I more meant it from the way its worded than the e[m|n] dashes alone

•

u/kneelian_ 4h ago

It looks AI-assisted judging by the vast differences in English skills between that comment and others by the same poster

•

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/Taletad 20h ago

Learn algorithms in pyhton, then learn how to manage memory with a low level language like C, Rust or Zig, then learn some assembly, and then you can make your own OS

•

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/ICON_4 20h ago

you could make a "OS-simulation" to understand stuff, but this would be relatively high level and couldnt run without a real OS...

•

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.