Compile Scala to .mpy?

Has anyone attempted to compile Scala code to .mpy?

(I did find Scalapy,, but I want to do the inverse . That project looks good but is thwarting my search.)



I don’t know of such a project, but what you could try, is a two-step approach: Use ScalaJS to compile Scala to JavaScript, then convert that to Python, i.e. using Js2Py.

1 Like

Do you want to access Java from Python or convert Scala code to Python code? The latter seems implausible.

For the first case for standard Python look at py4j.
For your “version” of Python I don’t know if it is possible.

I also suspect that because you want to work on embedded systems will also not work for you. But even if it did you still have to be aware that not all Python libraries may be accessed this way. @ansvonwa proposed solution may also have this problem.


Interesting – why? I can see that it might be an infeasible amount of work to do well, but we’ve got enough proof-of-concept cross-compiles at this point that it seems possible. And the proposed two-step approach seems like a worthwhile experiment, although limited in what it could accomplish.

1 Like

@jducoeur The problem is that its not just a question of mapping the simple language constructs like if and while, but you need to:

  • Convert programs constructs that don’t exist in the other language;
  • Adapt the data structures that are not available in the “host” language

I addition to this, the OP seems to want to use this on an embedded system. Which means you now have to cater for hardware restriction and most probably don’t have OS support.

I would say it would be pretty difficult to get this going. :cold_sweat:

Now that I think of it, if the embedded system uses Linux maybe Native Scala could cut it?

1 Like

The equivalent of ScalaJS, but for Python, wouldn’t be any easier or harder than JS I would imagine? Which is to say that it is not impossible, but just a whole lot of work. Of course, a big part of this work – namely, rewriting a lot of the Java stdlib in pure scala so it can be compiled, has already been done.

I would absolutely love to see such a project, so that Scala can stay relevant in the machine learning/data science world that increasingly relies on Python. But it’s definitely a large project.

Thanks all.

I’d hoped to find a project that already did most of the work - because of its value to the data science community. It looks like they mostly work the other way - running a JVM alongside python - with a few people running python within the JVM.

I’m working with students using Lego robots. Python isn’t a good first language for 10-year-olds, and they get frustrated with Scratch’s lack of abstractions.

The newer system - Spike Prime - runs the micropython OS ( ) which is a lot less to support; some things from the full JDK just wouldn’t make sense. I don’t think Scala Native would be viable there, but a similar approach could work.

(I originally tried getting Scala Native running on the older EV3 system, but Scala Native baked in 64-bit and the ARM cpu in the EV3 is 32-bit. I have had a lot of success - very good performance - using Scala on ev3dev OS’s 32-bit JVM.)

Maybe supporting micropython as an initial - smaller than everything - step would make sense, but I think it’s beyond what I can contribute to.

1 Like

It seems like Scala Native has 32-bit support in the pipe, by judging from:

The problem (for me) with waiting for Scala native 32-bit is … the waiting. It’ll be great when it is available, but the kids get older every year, and - now that Lego has stopped making the EV3 - the EV3s get older, more broken, and more expensive. I plan to use it when it’s available if I still have a working EV3 and kids willing to learn it. It’s a slow train coming.

Spike Prime - and micropython - are contemporary technologies in 2022, even if they are more limited.

For Lego robotics I wouldn’t need all of Java supported. The project is nearly pure Scala, with just a little device access via nio. On Spike Prime the device access is available from python; I’d just need some way to generate the right python to access the devices.

After a little digging - it looks like I could call compiled C code from the micropython OS. And could (with 32-bit support) compile Scala native code for that.

Has anyone called compiled Scala native (in its current 64-bit form) from python? That could address a broad front of problems.

Someone posted about this topic in r/scala (Reddit). I don’t know if this person succeeded but it might be worth to look for this thread.

I found a few things in progress in Scala Native. It’s not possible yet, but is work in progress.

See Allow to link as dynamic library by WojciechMazur · Pull Request #2145 · scala-native/scala-native · GitHub - for Scala Native to produce .o files that can be called from inside Python.

And Add support for 32-bit platforms through a linker-level switch by shadaj · Pull Request #1571 · scala-native/scala-native · GitHub for 32-bit Scala Native.