Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between entry_points/console_scripts and scripts in setup.py?

There are basically two ways to install Python console scripts to my path by setup.py:

setup(     ...     entry_points = {         'console_scripts': [             'foo = package.module:func',         ],     } ) 

and

setup(     ...     scripts = [         'scripts/myscript.sh'     ] ) 

What are the differences? I see the first approach allows me to choose nice, specific name for my script, but are there any other differences? Different original purposes, compatibility (setuptools, distutils, ...?), usage, ...? I am quite confused and a nice elaborated reply could help me (and probably also others) to properly understand all this.

Update: Since I asked the question PyPA published these cool docs on the topic.

like image 318
Honza Javorek Avatar asked Sep 13 '13 13:09

Honza Javorek


People also ask

What is Entry_points in setup py?

Entry points are a type of metadata that can be exposed by packages on installation. They are a very useful feature of the Python ecosystem, and come specially handy in two scenarios: 1. The package would like to provide commands to be run at the terminal. This functionality is known as console scripts.

What is scripts in setup py?

The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing.

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.

What is an entry point script?

In computer programming, an entry point is where the first instructions of a program are executed, and where the program has access to command line arguments.


1 Answers

The docs for the (awesome) Click package suggest a few reasons to use entry points instead of scripts, including

  1. cross-platform compatibility and
  2. avoiding having the interpreter assign __name__ to __main__, which could cause code to be imported twice (if another module imports your script)

Click is a nice way to implement functions for use as entry_points, btw.

like image 197
jfeala Avatar answered Oct 05 '22 08:10

jfeala