6

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

8
  • 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 ? Commented Nov 28, 2017 at 4: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 Commented Nov 28, 2017 at 4:40
  • using client_simulator.main() gives 'module' object has no attribute 'main' Commented Nov 28, 2017 at 4:43
  • 1
    Yes. if name == "main": this statement checks if the invoking call is from the same program or not. Commented Nov 28, 2017 at 4:44
  • 1
    When you ask a question about an exception, include the traceback. Commented Nov 28, 2017 at 6:54

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

Comments

9

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

import subprocess
subprocess.run('python D:/your_script.py')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.