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

PythonPackagesetup.py

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

The docs for the (awesome) Click package [suggest a few reasons][1] 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.

[1]: http://click.pocoo.org/dev/setuptools/ "dev docs for the Click package"

Solution 2 - Python

One key difference between these two ways of creating command line executables is that with the setuptools approach (your first example), you have to call a function inside of the script -- in your case this is the func inside of your module. However, in the distutils approach (your second example) you call the script directly (which allows being listed with or without an extension).

Solution 3 - Python

The setup tools entry point approach (#1) also has the benefit that on windows an .exe will be created that can be double clicked and invoked like a regular windows program. This is in addition to having a script placed in the bin path on posix-like systems.

Solution 4 - Python

One more difference is that when using console_scripts, my module's __init__ file was run. When just using scripts, the module __init__ was not run, only the script was run.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHonza JavorekView Question on Stackoverflow
Solution 1 - PythonjfealaView Answer on Stackoverflow
Solution 2 - Pythonplessthanpt05View Answer on Stackoverflow
Solution 3 - PythonKaushik GhoseView Answer on Stackoverflow
Solution 4 - PythonspacetherView Answer on Stackoverflow