52

I am trying to define a basic function in python but I always get the following error when I run a simple test program;

>>> pyth_test(1, 2)

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    pyth_test(1, 2)
NameError: name 'pyth_test' is not defined

Here is the code I am using for this function;

def pyth_test (x1, x2):
    print x1 + x2

UPDATE: I have the script called pyth.py open, and then I am typing in pyth_test(1,2) in the interpreter when it gives the error.

Thanks for the help. (I apologize for the basic question, I've never programmed before and am trying to learn Python as a hobby)


import sys
sys.path.append ('/Users/clanc/Documents/Development/')
import test


printline()



## (the function printline in the test.py file
##def printline():
##   print "I am working"
pppery
  • 3,731
  • 22
  • 33
  • 46
Lance Collins
  • 3,365
  • 8
  • 41
  • 56
  • 1
    If your call to the function is on line 1, then where is the function definition? – detly May 13 '11 at 03:08
  • The code that throws an error looks like it's in an interactive session, but the function code looks like it's part of a file. Is that right? If so, how did you import the file into the interactive session? Please edit your question and add more code to show the full interactive session. (The edit button is at the bottom of the question, under the tags.) – senderle May 13 '11 at 03:09

5 Answers5

57

Yes, but in what file is pyth_test's definition declared in? Is it also located before it's called?

Edit:

To put it into perspective, create a file called test.py with the following contents:

def pyth_test (x1, x2):
    print x1 + x2

pyth_test(1,2)

Now run the following command:

python test.py

You should see the output you desire. Now if you are in an interactive session, it should go like this:

>>> def pyth_test (x1, x2):
...     print x1 + x2
... 
>>> pyth_test(1,2)
3
>>> 

I hope this explains how the declaration works.


To give you an idea of how the layout works, we'll create a few files. Create a new empty folder to keep things clean with the following:

myfunction.py

def pyth_test (x1, x2):
    print x1 + x2 

program.py

#!/usr/bin/python

# Our function is pulled in here
from myfunction import pyth_test

pyth_test(1,2)

Now if you run:

python program.py

It will print out 3. Now to explain what went wrong, let's modify our program this way:

# Python: Huh? where's pyth_test?
# You say it's down there, but I haven't gotten there yet!
pyth_test(1,2)

# Our function is pulled in here
from myfunction import pyth_test

Now let's see what happens:

$ python program.py 
Traceback (most recent call last):
  File "program.py", line 3, in <module>
    pyth_test(1,2)
NameError: name 'pyth_test' is not defined

As noted, python cannot find the module for the reasons outlined above. For that reason, you should keep your declarations at top.

Now then, if we run the interactive python session:

>>> from myfunction import pyth_test
>>> pyth_test(1,2)
3

The same process applies. Now, package importing isn't all that simple, so I recommend you look into how modules work with Python. I hope this helps and good luck with your learnings!

onteria_
  • 68,181
  • 7
  • 71
  • 64
  • Thanks. That is helpful and it does work. How would I call the script from either another script or in the interactive session? So if I had the test.py file closed, how would I go about running the pyth_test fuction? Thanks! – Lance Collins May 13 '11 at 03:34
  • U can also do `import filename` then `filename.function()` too, so when you read the program you can know from which file the function came. In this case `import myfunction` then `myfunction.pyth_test(1,2)`. – m3nda Dec 30 '15 at 04:18
9

It works for me:

>>> def pyth_test (x1, x2):
...     print x1 + x2
...
>>> pyth_test(1,2)
3

Make sure you define the function before you call it.

AJ.
  • 27,586
  • 18
  • 84
  • 94
7

In python functions aren't accessible magically from everywhere (like they are in say, php). They have to be declared first. So this will work:

def pyth_test (x1, x2):
    print x1 + x2

pyth_test(1, 2)

But this won't:

pyth_test(1, 2)

def pyth_test (x1, x2):
    print x1 + x2
bluepnume
  • 16,460
  • 8
  • 38
  • 48
0

if working with IDLE installed version of Python

>>>def any(a,b):
...    print(a+b)
...
>>>any(1,2)
3
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
0

It would help if you showed the code you are using for the simple test program. Put directly into the interpreter this seems to work.

>>> def pyth_test (x1, x2):
...     print x1 + x2
... 
>>> pyth_test(1, 2)
3
>>> 
zellio
  • 31,308
  • 1
  • 42
  • 61