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.

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
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 is right, but if you absolutely do need to do that, use below antipattern
import subprocess
subprocess.run('python D:/your_script.py')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With