4

I have a file named client_simulator.py and it contains a Class named Client_simulator which simulates clients behavior.

I have another file named pool_manager.py and it has no class, it has one __main__ and lot of functions and basically, I want to call a method named generator of Client_simulator class from one of the methods of pool_manager.py.

enter image description here

the basic structure of client_simulator.py is as follows

class Client_simulator(object):
    def generator(self):

if __name__ == '__main__':
Client_simulator().generator()

the basic structure of file pool manager.py is as follows

def start_client_simulator():

     client_simulator.Client_simulator().generator()

if __name__ == "__main__":
   start_client_simulator()

I am getting the following error

'module' object is not callable

P.S: I want to call __main __ instead of `generator()', how to do that?

I am recently moving from java to python that's why having these basic doubts. Thanks in advance

Amarjit Dhillon
  • 2,718
  • 3
  • 23
  • 62
  • I don't understand . Even when you call the main , it had generator call , so it will be called right ? Is that what you want to call the main directly ? – Saranya Sridharan Nov 28 '17 at 04:38
  • 1
    can you please let me know, how to call main. For example, I tried `client_simulator.main()` but its giving me same error – Amarjit Dhillon Nov 28 '17 at 04:40
  • using client_simulator.main() gives 'module' object has no attribute 'main' – Amarjit Dhillon Nov 28 '17 at 04:43
  • 1
    Yes. if __name__ == "__main__": this statement checks if the invoking call is from the same program or not. – Saranya Sridharan Nov 28 '17 at 04:44
  • You aren't attempting to call a module in the code you show, so it's not clear where your error message is coming from. What are you running that produces the `'module' object is not callable` error, and what is the *exact* error message you get? – chepner Nov 28 '17 at 04:48
  • you should create a main function. Though that's not a good practice why not just call another function instead. There is an eval though it's always frowned upon – Jereme Nov 28 '17 at 04:49
  • 1
    When you ask a question about an exception, include the traceback. – Terry Jan Reedy Nov 28 '17 at 06:54
  • See this answer: https://stackoverflow.com/a/71967141/1364242 – Jay M Apr 22 '22 at 10:19

2 Answers2

9

I think you're confused a bit, at least in terminology if maybe not in code.

When you guard a section of code with if __name__ == "__main__":, you're not defining a __main__ function. It's just a normal if statement that reads the global variable __name__ (which is set up by the Python interpreter automatically) and compares its value to a string.

So there's no __main__ to be called from another module. If you want the contents of that block to be callable from elsewhere, you should put it in a function, which you can call from both the if __name__ == "__main__": block and from the other module, as necessary.

So try this in client_simulator.py:

class Client_simulator(object):
    def generator(self):

def main():    # you can name this whatever you want, it doesn't need to be main()
    Client_simulator().generator()

if __name__ == '__main__':
    main()

The pool_manager.py file can call client_simulator.main() too.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
6

@blckknght is right, but if you absolutely do need to do that, use below antipattern

import subprocess
subprocess.run('python D:/your_script.py')
Exis Zhang
  • 502
  • 6
  • 10