The docs for the (awesome) Click package suggest a few reasons to use entry points instead of scripts, including
- cross-platform compatibility and
- 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.
The docs for the (awesome) Click package suggest a few reasons to use entry points instead of scripts, including
- cross-platform compatibility and
- 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.
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).
python - How to set up entry_points in setup.cfg - Stack Overflow
entry_points console_scripts not working in setuptools >=69 if `dynamic` is not configured
Console_scripts entrypoints hidden behind extras are always installed
package - How to add console_script/entry point to setup.py that call a class function python - Stack Overflow
The section must be [options.entry_points]. See an example at https://github.com/github/octodns/blob/4b44ab14b1f0a52f1051c67656d6e3dd6f0ba903/setup.cfg#L34
[options.entry_points]
console_scripts =
octodns-compare = octodns.cmds.compare:main
octodns-dump = octodns.cmds.dump:main
octodns-report = octodns.cmds.report:main
octodns-sync = octodns.cmds.sync:main
octodns-validate = octodns.cmds.validate:main
With python 3.6 and setuptools 39.0.1, i had like you to move entry points from .py to .cfg.
My setup.py, focused on the entry points declaration:
from setuptools import setup
setup(
entry_points={
'pytest11': [
'mytest = mytest.plugin',
],
},
)
I ended up with this working setup.cfg:
... # other standard declarations
[options.entry_points]
pytest11 =
mytest = mytest.plugin
If there is not a good reason to not do so, I would definitely advocate a spin on option 3. As @jonsharp mentions, breaking up your utility into clean units of functionality is a good way to ensure testability. Even the smallest scripts can eventually morph into a much larger program and making sure that you have an extensible API sooner rather than later will alleviate much headache down the road.
The way I'd approach this is:
- Break up your code into logical methods with clean and clear I/O
- Add unit tests. Having them is never a bad thing.
- Rather than using
if __name__ == '__main__', create amain()(or similar) method containing your entry point - Use setuptool's
setup()function to define the script entry point in your setup.py file.
For example:
from setuptools import setup
setup(
name='mypackage',
version='0.1',
entry_points={
'console_scripts': [ 'myscript = mypackage.mymodule:main' ],
}
)
Now, not only is all of your code (including main()) is easily unit testable, but you can still have your console entry point once you've done a python setup.py install|develop.
Any validation I do using, say, ArgumentParser.add_mutually_exclusive_group will not apply when my tool is run as a library instead of a command line script.
Depending on how your API is designed, you may need to add some extra validation to input parameters, but that should likely be there to prevent unexpected input anyways.
Edit: The only time I would use generally use subprocess is when I'm calling into a non-Python application or another Python script that I don't own or have the time to refactor, but the latter only being as a last resort. Most well-written Python utilities will expose both command line utilities and internal API.
My guess is that you can pursue #3 by way of #2 and that it won't require anything close to a total refactor. Often in these situations, you just need to make a few adjustments at the entry points and (sometimes) exit points.
If needed, wrap your current script in a
main()function.def main(args, stdin = None): if stdin is None: stdin = sys.stdin # full script here if __name__ == '__main__': main(sys.argv[1:])That function should take a list of strings. When you parse command-line options, operate on
args, notsys.argv.Similarly, adjust other parts of your script to avoid direct operations on
sys.stdinand related streams. Instead, use the arguments passed to yourmain()function.
Programatic users (as opposed to command-line users) will pass in a list of strings and any open file handles (or iterables) they want the code to use. Later, if needed, you can make the programatic use more natural by writing a function that knows how to convert typical positional and keyword-style arguments into the list of strings that your option parser needs.