Search code examples
pythonfunctionmoduledistutilssetup.py

call python module functions from command line


I would like to know if there is a way to call module functions from the command line. My problem is that I have a file package called find_mutations. This requires that the input files are sorted in a specific way, so i made a function to do so. I would like the user to be able to have the option to run the code normally:

$ python -m find_mutations -h

or run the specific sort_files function:

$ python -m find_mutations.sort_files -h

Is something like this possible? If so, does the sort_files function need to be in its own strip? Do I need to add something to my __init__.py script? The package install properly and runs fine, I would just like to add this.


Solution

  • You could add flags to run the function. The following example is not a good way to work with command arguments, but demonstrates the idea.

    import sys
    
    if __name__ == "__main__":
        if '-s' in sys.argv:
            sort_files()
    

    Add a flag to run the specific function, in this case -s to sort the files.