24

I am working with an ARM Cortex M3 on which I need to port Python (without operating system). What would be my best approach? I just need the core Python and basic I/O.

3
  • 8
    Why do you want a high level language like Python but don't want an OS? Commented Mar 1, 2012 at 15:58
  • 5
    Whatever you're trying to do, I doubt python is a good fit for it.. Commented Mar 1, 2012 at 16:02
  • 10
    Consider a simpler language with fewer dependencies. Take a look at Lua. Commented Mar 1, 2012 at 17:17

6 Answers 6

23
+50

Golly, that's kind of a tall order. There are so many services of a kernel that Python depends upon, and that you'd have to provide yourself. I'd think you'd be far better off looking for a lightweight OS -- maybe Minix 3? -- to put on your embedded processor.

Failing that, I'd be horribly tempted to think about hand-translating to C and building the essentials on that.

Sign up to request clarification or add additional context in comments.

2 Comments

Negative. "There are so many services of a kernel that Python depends upon". No, Python is a language interpreter, not OS. Python uses some of OS-specific API, but they all can be either disabled or You may easily write Your own fake/dummy ones, then it will compile and basic functions should work. I did it and it works on platform for which Python was never exist(non-POSIX OS).
Well, if you don't think that sounds like kind of a tall order, I suspect the folks doing PyMite would like to see you do it.
14

You should definitely look at eLua:

http://www.eluaproject.net

"Embedded power, driven by Lua Quickly prototype and develop embedded software applications with the power of Lua and run them on a wide range of microcontroller architectures"

Comments

11

There are a few projects that have attempted to port Python to the situation you mention, take a look at python-on-a-chip, PyMite or tinypy. These are aimed at lower power microcontrollers without an OS and tend to focus on slightly older versions of the Python language and reduced library support.

1 Comment

Thanks. Just to be sure, "python-on-a-chip" and "PyMite" are the same thing, right?
5

One possible approach is to build your own stack machine in software to interpret and execute Python byte code directly. Certainly not a porting job and quite labor-intensive to implement, but a self-contained Python byte code stack processor built for your embedded system gets you around needing an operating system.

Another approach is writing your own low level executive (one step below a general purpose OS) that contains the bare minimum in services that a core Python interpreter port requires. I am not certain if this is more or less labor intensive than building a stack processor.

I am not recommending either of these approaches - personally, I like Charlie Martin's Minix 3 approach best since it is a balanced requirements compromise. On the other hand, what I suggest might be interesting if your project absolutely requires Python without an operating system and if the project has an excellent time and money budget.

Update 5 Mar 2012: Given a strict adherence to your Python/No OS requirements, another possibility of a path to a solution may lie in using an OS-less Java VM (e.g., jnode, currently in beta) and use Jython to create Java byte code from Python. Certainly not an ideal off-the-shelf solution, and it does seem to meet an OS-less Python requirement.

3 Comments

David, I don't think the byte-code interpreter would be enough -- python depends on lots of services and syscalls, eg, for I/O and scheduling.
@CharlieMartin You have an excellent point. Agreed, a straight CPython port requires a great deal in terms of system services. And agreed, a straight byte code interpreter requires the same. I was thinking perhaps a stack processor style of direct byte code execution would require less in the way of services since the nature of a stack processor forces some structure on execution. I was looking at Python byte code recently and it occurred to me that a Forth-like type of stack processor could be created to interpret a byte code stream. Might be too research level, though.
It'd be a fun project; the thing is that you still, someday, have to get down-and-dirty with your devices, which I suspect is where the worst of the code woud live.
4

Compile it to c :)

http://shed-skin.blogspot.com/

3 Comments

The result of compiling with shedskin itself needs to be compiled with a C++ compiler supported with libraries from an operating system target. So you've still got an OS requirement at the bottom of it all.
There are ARM Cortex M3 C++ compilers out there. Core python should(I'm not an expert so please tell me if I'm way off base) be supported without OS libraries and basic IO is going to be platform dependent no matter what you do.
Huh. On second thought, that's an interesting approach. So you write the low level executive or boot loader or simple services provider for the C++ produced by shed-skin. That's certainly no more difficult that what I proposed, and possibly less so. Thank you for pointing that out, 8bitwide.
1

fyi I just ported CPython 2.7x to non-POSIX OS. That was easy. You need write pyconfig.h in right way, remove most of unused modules. Disable unused features. Then fix compile, link errors. Then it just works after fixing some simple problems on run. If You have no some POSIX header, write one by yourself. Implement all POSIX functions, that needed, such as file i/o. Took 2-3 weeks in my case. Although I have heavily customized Python core. Unfortunately cannot opensource it :(.

After that I think Python can be ported easily to any platform, that has enough RAM.

4 Comments

How to write pyconfig.h?
@Meet Taraviya Hi! It's easy. Here is mine: pastebin.com/dvMgHUG4
^^^ for windows. + one for "non-POSIX": pastebin.com/3WjWcKG0. I can't include some non-opensource part , sorry. Basically You should disable all You can while it works. If OS doesn't have signal.h You disable it, write wrappers etc. I recommend to use Python 2.x, it's easier to port.
You are an angel!! I have been trying this for 2 days. Here's a treat for you : YouTube (Sorry in advance!!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.