Lets say i have a package folder called my_package. This directory contains a file called __init__.py and a module file called my_module.py among further module files. The module my_module.py contains the function do_something_with_file(file_path).
The function is used in other parts of the package but it would be practical if it could be called as a command line script that takes a file path as first positional argument and executes the function. My first naive approach was to implement that via:
if __name__ == "__main__":
import sys
do_something_with_file(sys.argv[1])
in the my_module.py file.
But that would require that the my_module.py is executed as main. But as part of a package it is using relative imports, which work only if the module is executed as part of a package but fail if the script is executed directly which makes this approach unattractive. It seems that a main block in this file is not the proper way to get the desired functionality.
What is good practice to expose the functionality as simple script?
I have no experience in packaging with python, I typically use scripts directly by executing python my_script.py but I'd like to get a better understanding of packages and exposing functionality with/via packages.