0

Im new to python and I want to print out the results from my add.py script on to my main.py script but I get an error "line 2, in print(add())TypeError: 'module' object is not callable"

main.py

import add
print(add())

add.py

class sample:
    def addition(self):
        add = 1+1
        return add
Sev
  • 31
  • 1
  • 8

1 Answers1

2

You want to change main.py to:

import add
print(add.sample().addition())

Please note in the second line:

add is the name of the module (file)

sample is the name of the class in the module. The parentheses are added to create a new instance of the class.

addition is the name of the method within the class

So in this line you are basically saying: Create a new instance of the class sample in the add module, then call the addition function on it.

The add variable in your addition function cannot be used outside of the function, because it's a local variable.

richflow
  • 1,902
  • 3
  • 14
  • 21