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
Honored Contributor II
Honored Contributor II

@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
Honored Contributor II
Honored Contributor II

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

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!