Calling __main__ from another file in python

10,322

Solution 1

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.

Solution 2

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

import subprocess
subprocess.run('python D:/your_script.py')
Share:
10,322

Related videos on Youtube

Amarjit Dhillon
Author by

Amarjit Dhillon

Check out my personal blog at https://medium.com/@smartsplash Twitter 🐦 https://twitter.com/smartsplash Linkedin 🔗 https://www.linkedin.com/in/amaardhillon/

Updated on May 30, 2022

Comments

  • Amarjit Dhillon
    Amarjit Dhillon almost 2 years

    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

    • Saranya Sridharan
      Saranya Sridharan over 6 years
      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 ?
    • Amarjit Dhillon
      Amarjit Dhillon over 6 years
      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
      Amarjit Dhillon over 6 years
      using client_simulator.main() gives 'module' object has no attribute 'main'
    • Saranya Sridharan
      Saranya Sridharan over 6 years
      Yes. if name == "main": this statement checks if the invoking call is from the same program or not.
    • chepner
      chepner over 6 years
      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?
    • Jereme
      Jereme over 6 years
      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
    • Terry Jan Reedy
      Terry Jan Reedy over 6 years
      When you ask a question about an exception, include the traceback.
    • Jay M
      Jay M about 2 years