Calling __main__ from another file in python
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')
Related videos on Youtube
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, 2022Comments
-
Amarjit Dhillon almost 2 years
I have a file named
client_simulator.py
and it contains a Class namedClient_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 namedgenerator
ofClient_simulator class
from one of the methods ofpool_manager.py
.the basic structure of
client_simulator.py
is as followsclass Client_simulator(object): def generator(self): if __name__ == '__main__': Client_simulator().generator()
the basic structure of file
pool manager.py
is as followsdef 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 over 6 yearsI 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 over 6 yearscan you please let me know, how to call main. For example, I tried
client_simulator.main()
but its giving me same error -
Amarjit Dhillon over 6 yearsusing client_simulator.main() gives 'module' object has no attribute 'main'
-
Saranya Sridharan over 6 yearsYes. if name == "main": this statement checks if the invoking call is from the same program or not.
-
chepner over 6 yearsYou 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 over 6 yearsyou 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 over 6 yearsWhen you ask a question about an exception, include the traceback.
-
Jay M about 2 yearsSee this answer: stackoverflow.com/a/71967141/1364242
-