Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros and cons of 'script' vs. 'entry_point' in Python command line scripts

Python's setuptool has two ways of adding command line scripts to a Python package: script and entry_point.

This tutorial outlines these ways:

scripts

Add a Python script (funniest-joke) to the package tree, and add its path to setup.py:

setup(
    ...
    scripts=['bin/funniest-joke'],
    ...
)

Entry point:

Add a Python script (funniest-joke) to the package tree. Add a main() function to it, and add command_line.py submodule which runs funniest's main():

command_line.py:

import funniest

def main():
    print funniest.joke()

setup.py

setup(
    ...
    entry_points = {
        'console_scripts': ['funniest-joke=funniest.command_line:main'],
    }
    ...
)

What are the advantages and disadvantages of each method?

like image 778
Adam Matan Avatar asked Apr 27 '14 14:04

Adam Matan


People also ask

What is Python Entrypoints?

Entry points are a way for Python packages to advertise objects with some common interface. The most common examples are console_scripts entry points, which define shell commands by identifying a Python function to run. Groups of entry points, such as console_scripts, point to objects with similar interfaces.

What is Entry_points in setup py?

An entry point is a Python object in a project's code that is identified by a string in the project's setup.py file. The entry point is referenced by a group and a name so that the object may be discoverable.

How do I run a Python script from console?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

What is console_scripts?

The console_scripts Entry Point Setuptools allows modules to register entrypoints which other packages can hook into to provide certain functionality. It also provides a few itself, including the console_scripts entry point.


1 Answers

Basically scripts is the old way which requires you to have a stand-alone, executable script file and the entry-points method lets you define which functions you want to run when a command is given. This way you can have several functions in the same file/module and then have 'entry points' which will be called when the user types in one of the console_scripts commands.

Although setup() supports a scripts keyword for pointing to pre-made scripts to install, the recommended approach to achieve cross-platform compatibility is to use console_scripts entry points (see below).

From https://packaging.python.org/tutorials/distributing-packages/#scripts (old source)

like image 99
Cargo23 Avatar answered Oct 14 '22 15:10

Cargo23