3 ms·
Way 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
by pmoriarty 9y ago
Way 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 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?
- greenshackle2 9y agoYou have to keep reading, further down they show the updated setup.py with the snek_types entry points. I agree it's not super well organized, they should have printed the new setup.py before that bit.
- Karunamon 9y agoThe registration is handled by the contents of the tuple in setup.py. Specifically: 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/#setup-args 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.
- lftl 9y agoThat seemed like magic to me at first too. I think though that is happens during the installation of the package, which is why he had to reinstall after refactoring. 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
- antoncohen 9y ago"Snek Pro Solutions" distributes a package (pip installed) called cute-snek. In the cute-snek setup.py it has: 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.