How to Call the main() Function of an Imported Module in Python
Last Updated :
23 Jul, 2025
We are given an imported module and our task is to call the main() function of that module after importing it in Python. In this article, we will see how to call the main() of an imported module in Python.
Call the main() Function of an Imported Module in Python
Below, are the code methods of how to call the main() of an imported module in Python:
- Import and Directly Call
main()
- Using
if __name__ == "__main__"
- Using
getattr()
to Dynamically Call main()
Import and Directly Call main() Function in Python
The most straightforward method involves importing the module and directly calling its main()
function. Let's consider a module named example_module
.
py
with the following structure:
example_module.py: below Python code defines a `main()` function in the module `example_module.py` and executes it only if the module is run directly, not when imported as a module into another script.
Python3
# example_module.py
def main():
print("Executing main() in example_module")
if __name__ == "__main__":
main()
main.py : below, code imports the module named `example_module` and directly calls its main() function, executing the code within the main() function of the imported module.
Python3
# main_script.py
import example_module
# Call the main() function directly
example_module.main()
Output
Executing main() in example_module
Using if __name__ == "__main__" in Python
In some cases, the main()
function may include command-line argument parsing or other logic that should only be executed when the module is run as the main program. To accommodate this, use the following structure in example_module.py
:
example_module.py: below Python code defines a main() function in the module example_module.py and executes it only if the module is run directly, not when imported as a module into another script.
Python3
# example_module.py
def main():
print("Executing main() in example_module")
if __name__ == "__main__":
main()
main.py: Below, code imports the module named example_module and calls its main() function only when the script is run directly, not when imported as a module into another script. The if __name__ == "__main__": condition ensures selective execution.
Python3
# main_script.py
import example_module
# Call the main() function using if __name__ == "__main__"
if __name__ == "__main__":
example_module.main()
Output
Executing main() in example_module
Using getattr()
to Dynamically Call main() Function
For more dynamic scenarios, where the module name or function to call may change at runtime, you can use the getattr()
function. Assume you have a variable module_name
containing the name of the module to import:
example_module.py: below Python code defines a main() function in the module example_module.py and executes it only if the module is run directly, not when imported as a module into another script.
Python3
# example_module.py
def main():
print("Executing main() in example_module")
if __name__ == "__main__":
main()
main.py : Below, code dynamically imports a module named "example_module" and dynamically calls its "main()" function using the getattr() function, allowing for flexibility in specifying module and function names at runtime.
Python3
# main_script.py
module_name = "example_module"
main_function_name = "main"
# Import the module dynamically
example_module = __import__(module_name)
# Dynamically call the main() function
getattr(example_module, main_function_name)()
Output
Executing main() in example_module
Conclusion
In conclusion, Calling the main()
function of an imported module in Python can be achieved through various methods, depending on your specific requirements. Whether you prefer a direct approach, utilize the if __name__ == "__main__"
condition, or dynamically call functions using getattr()
, Python offers flexibility to suit your coding style and project needs.
Similar Reads
How to Import Other Python Files? We have a task of how to import other Python Files. In this article, we will see how to import other Python Files. Python's modular and reusable nature is one of its strengths, allowing developers to organize their code into separate files and modules. Importing files in Python enables you to reuse
3 min read
How Can I Make One Python File Run Another File? In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Ma
2 min read
How to Run Another Python script with Arguments in Python Running a Python script from another script and passing arguments allows you to modularize code and enhance reusability. This process involves using a subprocess or os module to execute the external script, and passing arguments can be achieved by appending them to the command line. In this article,
3 min read
Run One Python Script From Another in Python In Python, we can run one file from another using the import statement for integrating functions or modules, exec() function for dynamic code execution, subprocess module for running a script as a separate process, or os.system() function for executing a command to run another Python file within the
5 min read
What is __Init__.Py File in Python? One of the features of Python is that it allows users to organize their code into modules and packages, which are collections of modules. The __init__.py file is a Python file that is executed when a package is imported. In this article, we will see what is __init__.py file in Python and how it is u
5 min read
How to Fix: ImportError: attempted relative import with no known parent package ImportError: attempted relative import with no known parent package error occurs when attempting to import a module or package using a relative import syntax, but Python is unable to identify the parent package. In this article, we will see how to solve this error in Python.What is ImportError: Atte
3 min read