Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a list of console_scripts defined in a Python package

I'm using setup_tools and setup.py to distribute a package of tools. The face of the package the user sees is a collection of command line scripts installed using the console_scripts entry point.

I would like to have an additional script that the user can execute to see a list of console_scripts that the package has installed.

Is there a principled way of doing this? One simple way is to copy the list from the 'console_scripts' entry in setup.py, but this is going to be error prone as the list has to be updated when the package is modified.

My current solution is to actually read the setup.py script and parse the console_scripts entry. This does not look Pythonic.

Hence my question if there is a principled way to do this. Thanks!

like image 479
Kaushik Ghose Avatar asked Oct 30 '22 09:10

Kaushik Ghose


1 Answers

This is possible using the pkg_resources module from setuptools and filtering for the package you're interested in.

import pkg_resources
def find_my_console_scripts(package_name):
    entrypoints = (ep for ep in pkg_resources.iter_entry_points('console_scripts')
                   if ep.module_name.startswith(package_name))
    return entrypoints

The returned EntryPoints have attributes for everything defined in setup.py such as name, module_name,attrs.

like image 153
Damien Ayers Avatar answered Nov 10 '22 00:11

Damien Ayers