stickytape

repository·master·Indexed 42 days ago

https://github.com/mwilliamson/stickytape

A tool used to convert a Python script and all its dependencies into a single-file Python script. It provides a CLI to bundle modules, manage search paths via --add-python-path, and handle dynamic imports using --add-python-module.

Tokens
1K
Snippets
4
Records
5
Agent score
4%

What's inside stickytape

  1. Convert a Python script into a single-file script

    master

    Use the stickytape CLI to bundle a script and its dependencies into one file. You can redirect stdout to a file or use the --output-file flag. Use --add-python-path to specify directories to search for modules.

    # Using stdout redirection
    stickytape scripts/blah --add-python-path . > /tmp/blah-standalone
    
    # Using the --output-file flag
    stickytape scripts/blah --add-python-path . --output-file /tmp/blah-standalone
  2. Important caveats when using stickytape

    master

    Because stickytape munges source files, be aware of the following limitations:

    • Encoding: Your source file should be encoded using UTF-8. In Python 3, if no encoding is declared in the first two lines, UTF-8 is assumed.
    • Imports: Your script must not have any from __future__ imports.
    • File Paths: Code relying on specific file locations (like __file__) will likely break because the script is no longer in its original directory structure.
    • Dynamic Imports: Stickytape cannot automatically detect dynamic imports; you must use --add-python-module to include them.
    • Non-Python Files: Only imported Python modules are included. Static data files (text files, images, etc.) that are not explicitly imported will be omitted.
  3. Configure stickytape CLI options

    master

    Stickytape provides several flags to control how dependencies are resolved and how the output file is structured:

    • --add-python-path <path>: Adds a directory to the search path for modules.
    • --output-file <path>: Writes the resulting single-file script directly to the specified path.
    • --python-binary <path>: Specifies a Python binary to use for determining sys.path (e.g., a virtualenv's python binary).
    • --add-python-module <module>: Explicitly includes a module that might be imported dynamically and cannot be automatically detected.
    • --copy-shebang: Copies the shebang from the original script to the output file. By default, stickytape uses #!/usr/bin/env python.
    # Example using a specific virtualenv python binary
    stickytape scripts/blah --python-binary _virtualenv/bin/python --output-file /tmp/blah-standalone
    
    # Example explicitly including a dynamic module
    stickytape scripts/blah --add-python-module blah.util
    
    # Example copying the original shebang
    stickytape scripts/blah --copy-shebang --output-file /tmp/blah-standalone
  4. Use the stickytape CLI to convert scripts

    master

    The stickytape CLI tool converts a Python script and its dependencies into a single, self-contained Python file.

    Usage: python -m stickytape <script> [options]

    Arguments:

    • script: The path to the entry point Python script you want to convert.

    Options:

    • --add-python-module <module>: Append a specific Python module to be included in the output. This can be used multiple times.
    • --add-python-path <path>: Append a directory to the Python search path to help locate dependencies. This can be used multiple times.
    • --python-binary <binary>: Specify the Python binary to use during the conversion process.
    • --output-file <file>: The path to the file where the resulting single-script content will be written. If omitted, the output is sent to stdout.
    • --copy-shebang: If provided, the shebang line from the original script will be copied to the output file.
    python -m stickytape my_script.py --output-file bundled_script.py --add-python-module my_dependency --copy-shebang