[Tutor] Packaging questions

Cameron Simpson cs at cskk.id.au
Sun Dec 12 18:03:45 EST 2021


On 12Dec2021 11:17, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>> What should I do if I want my pip installation to be a command line
>> application instead of an importable library of functions? Do I need a more
>> sophisticated setup.py file which installs an executable in usr/bin or
>> something?
>
>You do know about the
>
>    if __name__ == “__main__”:
>
>Idiom for making a module executable, right?
>
>That means an executable script and a module are the same thing.

Just to follow on, you don't even need that (though you probably want 
it). The dictionary you supply with the module package can contain an 
entry like this:

    'entry_points': {
        'console_scripts': [
            'your_command_name = your.module.name:command_function',
        ],
    },

which makes a command line script which calls a function of your choice.  
The example above would create a script called "your_command_name" which 
called the "command_function" function in the module "your.module.name" 
when it is invoked.

Remember that you're installing into a shared area and make sure that 
both your module names and the command name itself are not too generic 
and thus unlikely to collide with other installed things. An end user 
can always make an alias for a long command if they use it a lot and 
start finding the typing tedious.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list