'Create a script to execute another python file

I am creating a new Python package. It is a testing framework. The package will offer a console script and the console script shall be used to invoke the application.

Since this is a testing application the primary argument to the command will be Python file(testing logic). Assume the console script's name is test_auto

The test application is invoked as

$ test_auto ./main.py

Which does some initialization and then executes the python file exactly like python ./main.py would do. The main.py is end-user's script.

The problem is executing the supplied python file.

Solution I tried; is importing the python file(user-script) using importlib. This works and executes the user-script, but isn't exactly same as invoking from Python. For eg; if there is if __name__ == '__main__': block in the user-script, it wouldn't be executed as we are 'import'ing the user-script for executing it.

Is there any way to achieve executing the python file as if it is directly invoked.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source