Reply to comment
Python - Automatic Script Creation with setuptools
drks — Thu, 2009-04-02 06:35
Overview
This article is a continuation of Getting Started with setuptools.
entry_points are a dictionary mapping entry point group names to strings or lists of strings defining the entry points. Entry points are used to support dynamic discovery of services or plugins provided by a project. See Dynamic Discovery of Services and Plugins for details and examples of the format of this argument. In addition, this keyword is used to support Automatic Script Creation.
The following is an example setup.py that automatically creates our '/usr/bin/helloworld' (or similar) console script:
./setup.py:
#!/usr/bin/env python from setuptools import setup, find_packages import sys, os version = '0.1' setup(name='helloworld', version=version, description="My Hello World Application", long_description="""My even longer description about Hello World.""", classifiers=[], keywords='', author='Your Name', author_email='you@example.com', url='', license='', packages=find_packages(exclude=['ez_setup', 'examples', 'tests']), include_package_data=True, zip_safe=False, install_requires=[ # -*- Extra requirements: -*- ], entry_points=""" [console_scripts] helloworld = helloworld.core:main """ )
After creating the setup.py, I can now install it or because we are still working on it, we can use the setuptools develop mode feature:
$ sudo python setup.py develop running develop running egg_info writing helloworld.egg-info/PKG-INFO writing top-level names to helloworld.egg-info/top_level.txt writing dependency_links to helloworld.egg-info/dependency_links.txt writing entry points to helloworld.egg-info/entry_points.txt reading manifest file 'helloworld.egg-info/SOURCES.txt' writing manifest file 'helloworld.egg-info/SOURCES.txt' running build_ext Creating /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/helloworld.egg-link (link to .) helloworld 0.1 is already the active version in easy-install.pth Installing helloworld script to /opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin Installed /Users/you/helloworld Processing dependencies for helloworld==0.1 Finished processing dependencies for helloworld==0.1
Note: Using 'develop' rather than 'install' allows you to deploy your project in "development mode", such that it's available on sys.path, yet can still be edited directly from its source checkout.
We should now be able to call the command line utility 'helloworld' right away:
$ helloworld Hello World! My name is johnny.
Depending on the system you are working on (OSX in this example) depends on where the console script gets created to. On most Linux distributions this should end up being /usr/bin or /usr/local/bin. In my case it is in a deep, convoluted path that Apple chose. If we take a look at the script it creates you'll see how it is calling our application:
#!/usr/bin/env python # EASY-INSTALL-ENTRY-SCRIPT: 'helloworld==0.1','console_scripts','helloworld' __requires__ = 'helloworld==0.1' import sys from pkg_resources import load_entry_point sys.exit( load_entry_point('helloworld==0.1', 'console_scripts', 'helloworld')() )
And so... because we registered our helloworld.core.main() function as an entry point for the helloworld console script, when you run it the entry point is identified via pkg_resources.load_entry_point() and therefore main() is called.
Solid?
| Attachment | Size |
|---|---|
| helloworld-2.tar.gz | 780 bytes |
RSS Feed