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.
Installation
First, add ScalaPy to your SBT build:
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.
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.
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 py.Dynamic.global
, you can access any members of the global scope.
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.
Execution
ScalaPy officially supports Python 3.{7, 8, 9}. If you want to use another version of Python, you should define the environment variable SCALAPY_PYTHON_LIBRARY