Calling functions from other files

Hi everyone,

First of all, I’m not a programming expert, at all! This question might sound dumb to you, and I might not use the appropriate naming. To make things simple, I’ve written example files that will explain what I’m doing, and what I’m expecting.

py1.py

import getopt,sys

import py2,py3,py4

class class1:
    myPy2 = py2.class2()
    myPy3 = py3.class3()
    myPy4 = py4.class4()
    
    def function1(self):
        myPy4.function4('from py1')
        myPy3.function3()
        myPy2.function2()

    def describe_options(self):
        print ('Usage:')
        print ('-v|--verbose: enable verbose mode')
        print ('-h|--help: displays this help')


if __name__ == '__main__':
    myPy1 = class1()
    myPy2 = py2.class2()
    myPy3 = py3.class3()
    myPy4 = py4.class4()
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], "vh", ["verbose", "help"])
    except getopt.GetoptError as err:
        print (str(err))  # will print something like "option -a not recognized"
        print ("Using -h or --help might save you some time...")
        sys.exit(2)

    for the_option, the_arg in opts:
        # print "->"+the_option
        if the_option in ("-v", "--verbose"):
            myPy4.set_verbose(True)
        elif the_option in ("-h", "--help"):
            myPy1.describe_options()
            sys.exit(1)
        else:
            assert False, "unhandled option"
  
    myPy1.function1()

py2.py

import getopt,sys
import py3,py4

class class2:
    myPy3 = py3.class3()
    myPy4 = py4.class4()
    
    def function2(self):
        myPy4.function4('from py2')
        myPy3.function3()

    def describe_options(self):
        print ('Usage:')
        print ('-v|--verbose: enable verbose mode')
        print ('-h|--help: displays this help')


if __name__ == '__main__':
    myPy2 = class2()
    myPy3 = py3.class3()
    myPy4 = py4.class4()
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], "vh", ["verbose", "help"])
    except getopt.GetoptError as err:
        print (str(err))  # will print something like "option -a not recognized"
        print ("Using -h or --help might save you some time...")
        sys.exit(2)

    for the_option, the_arg in opts:
        # print "->"+the_option
        if the_option in ("-v", "--verbose"):
            myPy4.set_verbose(True)
        elif the_option in ("-h", "--help"):
            myPy2.describe_options()
            sys.exit(1)
        else:
            assert False, "unhandled option"
  
    myPy2.function2()

py3.py

import getopt,sys
import py4

class class3:
    myPy4 = py4.class4()
    
    def function3(self):
        myPy4.function4('from py3')

    def describe_options(self):
        print ('Usage:')
        print ('-v|--verbose: enable verbose mode')
        print ('-h|--help: displays this help')


if __name__ == '__main__':
    myPy3 = class3()
    myPy4 = py4.class4()
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], "vh", ["verbose", "help"])
    except getopt.GetoptError as err:
        print (str(err))  # will print something like "option -a not recognized"
        print ("Using -h or --help might save you some time...")
        sys.exit(2)

    for the_option, the_arg in opts:
        # print "->"+the_option
        if the_option in ("-v", "--verbose"):
            myPy4.set_verbose(True)
        elif the_option in ("-h", "--help"):
            myPy3.describe_options()
            sys.exit(1)
        else:
            assert False, "unhandled option"
  
    myPy3.function3()

and finally py4.py

import getopt,sys

class class4:
    
    def __init__(self):
        self.verbose = False
    
    def function4(self,text):
        if (self.verbose == True):
            print(text)
    
    def set_verbose(self,state):
        self.verbose = state

    def describe_options(self):
        print ('Usage:')
        print ('-v|--verbose: enable verbose mode')
        print ('-h|--help: displays this help')


if __name__ == '__main__':
    myPy4 = class4()
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], "vh", ["verbose", "help"])
    except getopt.GetoptError as err:
        print (str(err))  # will print something like "option -a not recognized"
        print ("Using -h or --help might save you some time...")
        sys.exit(2)

    for the_option, the_arg in opts:
        # print "->"+the_option
        if the_option in ("-v", "--verbose"):
            myPy4.set_verbose(True)
        elif the_option in ("-h", "--help"):
            myPy4.describe_options()
            sys.exit(1)
        else:
            assert False, "unhandled option"
  
    myPy4.function4('from py4')
  • function4() prints debug traces if verbose variable is set to True
  • function4() is called by other functions in py1.py, py2.py and py3.py

These are the errors I get :

I need to call function4() from any other files. What am I doing wrong?

Python versions :

  • Python 3.13.1 (Windows)
  • Python 3.11.2 (Linux/Raspberry Pi OS)

Well, I’ve solved this problem by myself. I just had to move the external classes declarations outside of the local class, just after the import statements, in all files.

Example :

import getopt,sys
import py2,py3,py4

myPy2 = py2.class2()
myPy3 = py3.class3()
myPy4 = py4.class4()

class class1:

    def function1(self):
        myPy4.function4('from py1')
        myPy3.function3()
        myPy2.function2()

    def describe_options(self):
        print ('Usage:')
        print ('-v|--verbose: enable verbose mode')
        print ('-h|--help: displays this help')


if __name__ == '__main__':
    myPy1 = class1()
    myPy2 = py2.class2()
    myPy3 = py3.class3()
    myPy4 = py4.class4()
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], "vh", ["verbose", "help"])
    except getopt.GetoptError as err:
        print (str(err))  # will print something like "option -a not recognized"
        print ("Using -h or --help might save you some time...")
        sys.exit(2)

    for the_option, the_arg in opts:
        # print "->"+the_option
        if the_option in ("-v", "--verbose"):
            myPy4.set_verbose(True)
        elif the_option in ("-h", "--help"):
            myPy1.describe_options()
            sys.exit(1)
        else:
            assert False, "unhandled option"
  
    myPy1.function1()

Result with all above files modified :

Have a happpy and safe new year’s eve!

1 Like