Python Entry Points Explained
amir.rachum.com
Python Entry Points Explained
1–10 of 14 posts
Re: Python Entry Points Explained
#2[1] - https://docs.openstack.org/stevedore/latest/index.html
Re: Python Entry Points Explained
#3I got stumped at:
"In particular, the magic happens in get_sneks. The call to pkg_resources.iter_entry_points('snek_types') iterates over all the entry points that were registered under the name "snek_types". So, external packages can define an entry point called "snek_types" in their setup.py, and snek will dynamically load it at runtime."
Wait. What entry points were registered under the name "snek_types"? Where were they regeistered as such? I think I must be missing something, or maybe that registration was hidden somewhere among all the fancy snakes. Can someone help explain this in a clearer way?
Re: Python Entry Points Explained
#4Way too much ink wasted on cuteness and too little on explanation. I got stumped at: "In particular, the magic happens in get_sneks. The call to pkg_resources.iter_entry_points('snek_types') iterates over all the entry points that were registered under the name "snek_types". So, external packages can define an entry point called "snek_types" in their setup.py, and snek will dynamically load it at runtime." Wait. What…
I agree it's not super well organized, they should have printed the new setup.py before that bit.
Re: Python Entry Points Explained
#5Way too much ink wasted on cuteness and too little on explanation. I got stumped at: "In particular, the magic happens in get_sneks. The call to pkg_resources.iter_entry_points('snek_types') iterates over all the entry points that were registered under the name "snek_types". So, external packages can define an entry point called "snek_types" in their setup.py, and snek will dynamically load it at runtime." Wait. What…
from setuptools import setup
setup(
name='snek',
entry_points={
'console_scripts': [
'snek = snek:main',
],
}
)
It's using the setuptools module, and passing a tuple to it - which is used to create executables for the individual scripts once installed.https://packaging.python.org/tutorials/distributing-packages...
Specifically, "entry_points" in the setup tuple is what sets the name of the created files and links them to your code. In the example, the executable 'snek' points to main() in snek.py.
Once your package is installed, pip or easy_install handles the magic of putting those files in place. There are boatloads of other options available (much like any packaging system), but this is a minimum viable example.
Re: Python Entry Points Explained
#6Way too much ink wasted on cuteness and too little on explanation. I got stumped at: "In particular, the magic happens in get_sneks. The call to pkg_resources.iter_entry_points('snek_types') iterates over all the entry points that were registered under the name "snek_types". So, external packages can define an entry point called "snek_types" in their setup.py, and snek will dynamically load it at runtime." Wait. What…
I initially just skimmed over his log dump from the install, but I think the details are actually important there:
> writing entry points to cute_snek.egg-info\entry_points.txt
Re: Python Entry Points Explained
#7Way too much ink wasted on cuteness and too little on explanation. I got stumped at: "In particular, the magic happens in get_sneks. The call to pkg_resources.iter_entry_points('snek_types') iterates over all the entry points that were registered under the name "snek_types". So, external packages can define an entry point called "snek_types" in their setup.py, and snek will dynamically load it at runtime." Wait. What…
entry_points={
'snek_types': [
'cute = cute_snek:cute_snek',
],
}
The main snek codebase has: for entry_point in pkg_resources.iter_entry_points('snek_types'):
sneks[entry_point.name] = entry_point.load()
pkg_resources.iter_entry_points() iterates through all the snek_types an end user has installed on their system. When the end user does `pip install snek` they will only have the built-in sneks, when they also do `pip install cute-snek` the will have the "cute" snek installed, allowing them to run `snek --type cute` to see it.Re: Python Entry Points Explained
#8Compare
$ time python -m pyflakes /dev/null
real 0m0.225s
user 0m0.103s
sys 0m0.008s
with $ time pyflakes /dev/null
real 0m2.468s
user 0m1.738s
sys 0m0.055sRe: Python Entry Points Explained
#9Entry points are excellent at making your scripts start slooooooooow. Compare $ time python -m pyflakes /dev/null real 0m0.225s user 0m0.103s sys 0m0.008s with $ time pyflakes /dev/null real 0m2.468s user 0m1.738s sys 0m0.055s
Re: Python Entry Points Explained
#10Entry points are excellent at making your scripts start slooooooooow. Compare $ time python -m pyflakes /dev/null real 0m0.225s user 0m0.103s sys 0m0.008s with $ time pyflakes /dev/null real 0m2.468s user 0m1.738s sys 0m0.055s
I've noticed that using argparse makes my scripts start very slow, especially if you expose a large number of option. I have a script that I wrote for work that has quite a variable number of available options and it can sometimes take several seconds for the script to startup and actually begin doing anything useful.
For me, it is not debugable. I wanted to propose and start a reference implementation for supporting --[no-]bool-opt, but I quickly gave up. There's some solutions on SO, but none of them are sufficient. The best solution makes --bool-opt mutually exclusive to --no-bool-opt which will cause a hard failure which is not okay for shell aliases.