Search code examples
scalaread-eval-print-loop

How do I make packages available to the Scala REPL?


I'm trying to get familiar with Scala. I am using macOS.

I've installed scala using brew install scala which is hassle-free and once complete I can launch the scala REPL simply by issuing scala and I'm at the scala> prompt.
I now want to import some packages, so I try:

import org.apache.spark.sql.Column

and unsurprisingly it fails with

error: object apache is not a member of package org

This makes sense, how would it know where to get that package from? Thing is, I don't know what I need to do to make that package available. Is there anything I can do from the command-line that would allow me to import org.apache.spark.sql.Column?

I have googled around a little but not found anything that explains in a jargon-free way. Complete Scala noob here so jargon-free responses would be appreciated.


Solution

  • Here are two ways to start a REPL with dependencies that I'm aware of:

    1. Use SBT to manage dependencies, use console to start a REPL with those dependencies
    2. Use Ammonite REPL

    You could create a separate directory with a build.sbt where you set

    scalaVersion := "2.11.12"
    

    and then copy the

    libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.3.0"
    

    snippets from MavenCentral. Then you can run the REPL with sbt console. Note that this will create a project and target subdirectories, so it "leaves traces", you can't use it like the standalone scala-repl. You could also omit the build.sbt, and add the library-dependencies by typing them into the SBT-shell itself.

    Alternatively you can just use Ammonite REPL that has been created exactly for that purpose.