# NameError: name ‘REF’ is not defined

**URL:** <https://forum.anaconda.com/t/nameerror-name-ref-is-not-defined/79735>\
**Category:** Anaconda Code\
**Created:** [August 7, 2024, 7:17pm UTC](https://forum.anaconda.com/t/nameerror-name-ref-is-not-defined/79735 "2024-08-07T19:17:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.sime](https://avatars.discourse-cdn.com/v4/letter/p/db6015/32.png) [@peter.sime](https://forum.anaconda.com/u/peter.sime)\
**Post date:** [August 7, 2024, 7:17pm UTC](https://forum.anaconda.com/t/nameerror-name-ref-is-not-defined/79735/1 "2024-08-07T19:17:31Z")

</div>

am trying this in the excel add-in

# Add imports

import math  
import numpy as np  
import pandas as pd

# Define classes and functions

def to\_df(data) → pd.DataFrame:  
“”"  
Converts a REF (i.e. list of lists) into a pandas DataFrame  
Assumes that the first row contains column headers

```
Usage: df = to_df(REF("F15:H22"))
"""
return pd.DataFrame(data[1:], columns=data[0])

```

def to\_array(data) → np.ndarray:  
“”"  
Converts a REF (i.e. list of lists) into a 2-dimensional numpy array

```
Usage: arr = to_array(REF("B3:C44"))
"""
return np.array(data)

```

test\_df = to\_df(REF(“A1:L30”))  
print(test)

get this in logs.. I am missing something obvious or misusing ?  
Traceback (most recent call last):  
File “Imports and Definitions”, line 24, in   
test\_df = to\_df(REF(“A1:L30”))

---

<div class="post-metadata">

**Author:** ![peter.sime](https://avatars.discourse-cdn.com/v4/letter/p/db6015/32.png) [@peter.sime](https://forum.anaconda.com/u/peter.sime)\
**Post date:** [August 7, 2024, 7:18pm UTC](https://forum.anaconda.com/t/nameerror-name-ref-is-not-defined/79735/2 "2024-08-07T19:18:22Z")

</div>

NameError: name ‘REF’ is not defined

---

<div class="post-metadata">

**Author:** ![Jim\_Kitchen](https://avatars.discourse-cdn.com/v4/letter/j/f5a800/32.png) [@Jim\_Kitchen](https://forum.anaconda.com/u/Jim_Kitchen)\
**Post date:** [August 7, 2024, 11:32pm UTC](https://forum.anaconda.com/t/nameerror-name-ref-is-not-defined/79735/3 "2024-08-07T23:32:36Z")

</div>

`REF` cannot be used within the Imports and Definitions.

`REF` is a special piece of machinery that allows Anaconda Code cells to refer to values in other Excel cells.

Normally, PyScript is not aware that it is being run within Excel. It cannot arbitrarily access the .xlsx file nor the running Excel instance. Instead, Anaconda Code looks for `REF()` calls, then injects the appropriate data into a dictionary within the PyScript runtime. After that, it executes the actual Python code which grabs the value from the dictionary and returns a value.

Imports and Definitions is meant to pre-load libraries and functions into the PyScript runtime exactly once. Afterwards, it does not re-run as individual Python cells are run. As a result, if the values of a `REF` defined within Imports and Definitions changed, that change would not get picked up by the runtime. That is why `REF` is disabled within Imports and Definitions.

---

<div class="post-metadata">

**Author:** ![Jim\_Kitchen](https://avatars.discourse-cdn.com/v4/letter/j/f5a800/32.png) [@Jim\_Kitchen](https://forum.anaconda.com/u/Jim_Kitchen)\
**Post date:** [August 7, 2024, 11:36pm UTC](https://forum.anaconda.com/t/nameerror-name-ref-is-not-defined/79735/4 "2024-08-07T23:36:01Z")

</div>

Typical usage is:

1. Go to the Anaconda Code dashboard
2. Click `+ New` to create a new Code cell
3. In the editor, click `Link Range` and choose the cells to reference (e.g. A1:L30).

The code in the editor will automatically write the text `REF("A1:L30")`. Write the same code you were trying to test and it should work.

---

<div class="post-metadata">

**Author:** ![peter.sime](https://avatars.discourse-cdn.com/v4/letter/p/db6015/32.png) [@peter.sime](https://forum.anaconda.com/u/peter.sime)\
**Post date:** [August 8, 2024, 12:12am UTC](https://forum.anaconda.com/t/nameerror-name-ref-is-not-defined/79735/5 "2024-08-08T00:12:16Z")

</div>

Thanks Jim, my error was very dumb, but I have been catching up on Owen’s video series and going back to basics on python in excel… walk before trying to run…

---

<div class="post-metadata">

**Author:** ![Jim\_Kitchen](https://avatars.discourse-cdn.com/v4/letter/j/f5a800/32.png) [@Jim\_Kitchen](https://forum.anaconda.com/u/Jim_Kitchen)\
**Post date:** [August 8, 2024, 3:22pm UTC](https://forum.anaconda.com/t/nameerror-name-ref-is-not-defined/79735/6 "2024-08-08T15:22:31Z")

</div>

It’s definitely a learning curve! I’m glad you’re starting on the journey.
