Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function is not defined error in Python

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" 
like image 997
Lance Collins Avatar asked May 13 '11 03:05

Lance Collins


People also ask

Why is Python saying my variable is not defined?

This is because when you declare a variable or a function, Python stores the value with the exact name you have declared. If there is a typo anywhere that you try to reference that variable, an error will be returned.

How do I fix name errors in Python?

You can fix this by doing global new at the start of the function in which you define it. This statement puts it in the global scope, meaning that it is defined at the module level. Therefore, you can access it anywhere in the program and you will not get that error.

How do you define a function in Python?

Basic Syntax for Defining a Function in Python In Python, you define a function with the def keyword, then write the function identifier (name) followed by parentheses and a colon. The next thing you have to do is make sure you indent with a tab or 4 spaces, and then specify what you want the function to do for you.

How do you define a name error in Python?

NameError is a kind of error in python that occurs when executing a function, variable, library or string without quotes that have been typed in the code without any previous Declaration. When the interpreter, upon execution, cannot identify the global or a local name, it throws a NameError.


2 Answers

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!

like image 173
onteria_ Avatar answered Sep 23 '22 22:09

onteria_


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.

like image 21
AJ. Avatar answered Sep 20 '22 22:09

AJ.