Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / function is not defined error in Python

function is not defined error in Python

August 19, 2021 by James Palmer

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
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!

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.

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in