0

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

0

2 Answers 2

1

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.

1
  • 1
    Thanks so much Jonathon March - I knew it would be something this simple! My headache has gone no! Thanks! Jan 25, 2015 at 22:24
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.