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: 

Why am I getting NameError name ' ' is not defined in another cell?

Ank
New Contributor II

I defined a dictionary variable Dict, populated it, and print(dict) in the first cell of my notebook. In the next cell, I executed the command print(dict) again. However, this time it gave me an error

NameError: name 'Dict is not defined

How can that be? Why am I able to print this variable in one cell, but unable to do so in the next cell?

5 REPLIES 5

Morg
New Contributor II

I am facing the same issue while executing a job currently. Was a cause/solution found for this?

Tharun-Kumar
Databricks Employee
Databricks Employee

@Ank @Morg 

One possible scenario, when this could happen is the variable (dict) was defined in a python environment and it was called in a scala environment or the vice versa.

Screenshot 2023-08-01 at 10.14.23 AM.png

Tharun-Kumar
Databricks Employee
Databricks Employee

A variable defined in a particular language environment will be available only in that environment. It will not be available in other environments. Can you confirm?

@Ank @Morg 

Morg
New Contributor II

I can confirm that your answer at least pointed me in the right direction.

It seems my issue wasn't exactly the same as I was instead using the magic pip command to install a notebook scoped package:
%pip install <some-package>

Anything defined within that command cell seems to be lost so I had to separate this into it's own command before everything else.

Thank you for the help!

erigaud
Honored Contributor

Running pip install restarts the interpreter, meaning that any variable defined prior to the pip install is lost, so indeed the solution is so run the pip install first, or better is to add the library you want to installl directly to the cluster configuration

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