Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618802
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:20:25+00:00 2026-05-22T02:20:25+00:00

I am trying to define a basic function in python but I always get

  • 0

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"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:20:26+00:00Added an answer on May 22, 2026 at 2:20 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dynamically define functions that call through to another function that takes
I am trying to figure out how to define a function that works on
Im trying to define a dataTemplate for a business object in my wpf application
I'm trying to define a task that emits (using echo) a message when a
I'm trying to define a table to store student grades for a online report
I am trying to define a WCF contract that returns an interface, something like
I'm trying to generate a CSS class that will help me define a page
I'm trying to learn scheme via SICP. Exercise 1.3 reads as follow: Define a
I'm trying to select data from a table defined similar to the following :
I have the following members defined in a class that I'm trying to deserialise:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.