Mark Turner
Mark Turner

Reputation: 3

Enthought Canopy python - name ' ' not defined

I'm very new to using canopy and programming in general.

I'm trying to define a function in Python in the canopy editor. This used to work for me but has suddenly stopped and I have no idea why.

As a basic example, in the editor I wrote;

def funct(x):
    return x

When write funct(1) in the shell I get the error message

NameError: name 'funct' is not defined

Any ideas? Thanks

Upvotes: 0

Views: 1014

Answers (2)

Jonathan March
Jonathan March

Reputation: 5810

You need to "run" your script (in the editor) before its results actually exist (and are visible in) the Python shell. In this case the results of your script are to define your function. Just writing the function in the editor doesn't actually create it in Python until you run the script.

As Ali correctly said, another (deeper) approach is to import the script (in this case known as a module), but I think running is probably more what you have in mind.

Upvotes: 1

ali_m
ali_m

Reputation: 74172

I've never used Canopy before, but in general you would save the file where your function is defined somewhere in your working directory (e.g. as myfunct.py), then import it into the shell namespace:

In  [1]: import myfunct

In  [2]: myfunct.funct(1)
Out [2]: 1

Upvotes: 0

Related Questions