cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Can we access the variables created in Python in Scala's code or notebook ?

Zircoz
New Contributor II

If I have a dict created in python on a Scala notebook (using magic word ofcourse):

%python
d1 = {1: "a", 2:"b", 3:"c"}

Can I access this d1 in Scala ?

I tried the following and it returns d1 not found:

%scala
println(d1)

1 ACCEPTED SOLUTION

Accepted Solutions

MartinB
Contributor III

Variables are not shared between language contexts.

Ugly workaround: you could do something like this to pass your python variable to the spark context:

%python
d1 = {1: "a", 2:"b", 3:"c"}
spark.conf.set('d1', str(d1))
%scala
println( spark.conf.get("d1") )

However, you lose variable type. You only can pass on strings.

View solution in original post

2 REPLIES 2

MartinB
Contributor III

Variables are not shared between language contexts.

Ugly workaround: you could do something like this to pass your python variable to the spark context:

%python
d1 = {1: "a", 2:"b", 3:"c"}
spark.conf.set('d1', str(d1))
%scala
println( spark.conf.get("d1") )

However, you lose variable type. You only can pass on strings.

cpm1
New Contributor II

Martin is correct. We could only access the external files and objects. In most of our cases, we just use temporary views to pass data between R & Python.

https://docs.databricks.com/notebooks/notebooks-use.html#mix-languages

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group