Skip to main content

Makes entry_points specified in setup.py load more quickly

Project description

Using entry_points in your setup.py makes scripts that start really slowly because it imports pkg_resources, which is a horrible thing to do if you want your trivial script to execute more or less instantly. check it out: https://github.com/pypa/setuptools/issues/510

importing fastentrypoints in your setup.py file produces scripts that look like this:

# -*- coding: utf-8 -*-
import re
import sys

from package.module import entry_function

if __name__ == '__main__':
  sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
  sys.exit(entry_function())

This is ripped directly from the way wheels do it and is faster than whatever the heck the normal console scripts do.

Note:

This bug in setuptools only affects packages built with the normal setup.py method. Building wheels avoids the problem and has many other benefits as well. fastentrypoints simply ensures that your user scripts will not automatically import pkg_resources, no matter how they are built.

Usage

To use fastentrypoints, simply copy fastentrypoints.py into your project folder in the same directory as setup.py, and import fastentrypoints in your setup.py file. This monkey-patches setuptools.command.easy_install.ScriptWriter.get_args() in the background, which in turn produces simple entry scripts (like the one above) when you install the package.

If you install fastentrypoints as a module, you have the fastep executable, which will copy fastentrypoints.py into the working directory (or into a list of directories you give it as arguments) and append include fastentrypoints.py to the MANIFEST.in file, and add an import statement to setup.py. It is available from PyPI.

You can’t really make it a proper dependency because setuptools has to import it to work, so chicken-egg. right? Luckily, the script is trivial and will not hurt you project much to copy this 60-line file into the folder. Be sure to add it to MANIFEST.ini if you want to distributie it on PyPI.

Alternatively, if you don’t want to vendor the code, it is possible to install it from pypi with easy_install in the setup script:

try:
    import fastentrypoints
except ImportError:
    from setuptools.command import easy_install
    import pkg_resources
    easy_install.main(['fastentrypoints'])
    pkg_resources.require('fastentrypoints')
    import fastentrypoint

Let me know if there are places where this doesn’t work well. I’ve mostly tested it with console_scripts so far, since I don’t write the other thing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastentrypoints-0.12.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

fastentrypoints-0.12-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file fastentrypoints-0.12.tar.gz.

File metadata

File hashes

Hashes for fastentrypoints-0.12.tar.gz
Algorithm Hash digest
SHA256 ff284f1469bd65400599807d2c6284d5b251398e6e28811f5f77fd262292410b
MD5 390ad9a9229164a06156a5b1f0ef1b22
BLAKE2b-256 565969d9ae590ca39435d409651314ec13b2abe8127c1db0231c01d034ebb6b0

See more details on using hashes here.

File details

Details for the file fastentrypoints-0.12-py3-none-any.whl.

File metadata

File hashes

Hashes for fastentrypoints-0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 8db67b850993b55bb987e45dbb5c06db8deae0e9a1562c40701395bee473ef76
MD5 c94478420ad96368ca94e9e622b42aea
BLAKE2b-256 717c2f169d21a9d1f24df858c744a348145cf766a1c6a480db7f53070ebd2712

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page