Getting Started with ScalaPy

ScalaPy makes it easy to use Python libraries from Scala code. With a simple API, automatic conversion between Scala and Python types, and optional static typing, ScalaPy scales from hobby projects to production systems.


First, add ScalaPy to your SBT build:

libraryDependencies += "me.shadaj" %% "scalapy-core" % "0.4.0"

You'll then need to add the Python native libraries to your project and configure SBT to run your code in a separate JVM instance.

fork := true
import scala.sys.process._
javaOptions += s"-Djava.library.path=${"python3-config --prefix".!!.trim}/lib"

If you'd like to use Scala Native, follow the instructions there to create a project with Scala Native 0.4.0-M2. Then, add the following additional configuration to your SBT build to link the Python interpreter.

import scala.sys.proces._
nativeLinkingOptions ++= "python3-config --ldflags".!!.split(' ').map(_.trim).filter(_.nonEmpty).toSeq

Hello World!

Now that ScalaPy is installed, let's start with a simple example. ScalaPy offers a dynamically typed API that's great for making quick Python calls with little ceremony. First, we can use the Python len function to calculate the length of a list. Using, you can access any members of the global scope.

val listLengthPython =, 2, 3).toPythonProxy)
// listLengthPython: py.Dynamic = 3

Here, we took a Scala List, converted it to a Python list, sent it to the len function, and got back a Python number.

To convert Python values back into Scala, we use the .as method and pass in the type we want.

val listLength =[Int]
// listLength: Int = 3