Running Python code in your Smallworld session
October 12, 2026 smallworld-interop
Haven’t you ever wanted to run Python code in the same Smallworld session as you’re running your Magik code in? You really did, didn’t you?! Right, perhaps, maybe…? Anyway, now it is possible using sw_python_bridge!
A bit of background first. Python is actually a language described by the Python Language Reference, and when you run Python you’re most likely using the CPython implementation. Other implementations exist, such as PyPy, GraalPy, IronPython, and Jython.
Smallworld 5 introduced support for Java interop. This allows one to write parts of a program in Java, and then call into this code from Magik. The GNM product includes some demos for this under the core/interop.demo-directory. Even though this has been supported for some time now, only (very) minor parts are usually implemented in Java. For example, I’ve seen multiple companies implement an HTTP client using the Java interop when Smallworld/Magik did not have an HTTP client yet (these days there is sw:http.) Though, the sw:http implementation is opinionated and older versions of Smallworld 5 still required a custom version due to bugs in the client.
As you can guess by now, Jython is being used to run Python from within a Smallworld session by sw_python_bridge. Jython is a Java implementation of the mentioned Python Language Reference. It is used in multiple products where some kind of scripting is wanted. For example, IBM WebSphere uses Jython to provide administrative scripting capabilities. Or, Apache Pig uses Jython to support user defined functions. Jython only supports Python 2.7, however, which has been end of life since 2020. Also, it only supports pure Python libraries. So don’t expect to be able to use NumPy. But on the upside, Jython can use Java classes directly.
The cool thing is that it is very easy to start a Python interpreter from your Smallworld session. From there, you get the familiar Python REPL where you can run Python code. For example:
Magik> python.repl()
Python (Jython 2.7) via sw_python_bridge - Ctrl-D returns to Magik
>>> a = [20, 10, 0]
>>> a_sorted = sorted(a)
>>> print(a_sorted)
[0, 10, 20]
>>> ^D
Magik>
Even better, there is interop between Magik and Python. E.g., you can use Magik exemplars and call Magik methods from Python:
Magik> python.repl()
Python (Jython 2.7) via sw_python_bridge - Ctrl-D returns to Magik
>>> import sw
>>> r = sw.rope.new()
>>> r.add_last("hello")
>>> len(r)
1
>>> magik.eval("date.today().year")
2026
>>> ^D
Magik>
And vice versa:
## Import a module and call its functions
Magik> math << python.import_module("math")
a python_object_wrapper
Magik> write(math.factorial(10))
3628800
Magik> write(math.sqrt(2.0))
1.414
## Python objects come back as thin proxies: unknown selectors forward to Python,
## so plain Python methods just work
Magik> lst << python.eval_wrapped("[3, 1, 2]")
a python_object_wrapper
Magik> lst.sort()
unset
Magik> write(lst[1]) ## indexing is 1-based, Magik-style
1
Magik> write(lst.size)
3
Note that Python uses 0-based indexing, and Magik uses 1-based indexing. The bridge converts between these worlds.
Python describes a data model. It describes how objects, built-in functions, etc. work. It includes things like len(), indexing, iteration, arithmetic. The data model methods are often called “dunder” methods, for the double underscores. For example __len__, __getitem__ and __iter__.
To make Magik objects behave the way Python code expects, the data model is supported by sw_python_bridge. You can customize behavior by implementing the relevant methods on the Magik exemplars. For example, in Python you would use len(my_list) to get the length (size) of your list. This in turn calls the method __len__ on the object. To support Python’s len on the Magik objects, you would implement the method py!__len__ on the relevant exemplar(s):
_pragma(classify_level=restricted, topic={python_bridge})
_method basic_collection_mixin.py!__len__()
## Python len(self) -> size.
## @return {sw:integer}
_return _self.size
_endmethod
$
So why build this? It is mostly a tech demo. Also, I’ve been asked more than once whether something like this is possible. Smallworld developers are scarce compared to Python developers, so letting Python developers contribute could help. Some companies even want to rewrite their Smallworld application entirely in a language like Python; a bridge would make that transition less painful. And of course, it is fun to build something like this.
Did you ever want to run Python code from your Smallworld session? Maybe you didn’t, but now you can! The code and instructions on how to start can be found in the sw_python_bridge repository on GitHub. Feel free to contact me if you have questions, feedback, or interest.