Giter Site home page Giter Site logo

stickytape's Introduction

stickytape: Convert Python packages into a single script

Stickytape can be used to convert a Python script and any Python modules it depends on into a single-file Python script. There are likely better alternatives depending on what you're trying to do. For instance:

  • If you want to create a single file that can be executed by a Python interpreter, use zipapp.
  • If you need to create a standalone executable from your Python script, I recommend using an alternative such as PyInstaller.

Since Stickytape relies on correctly analysing both your script and any dependent modules, it may not work correctly in all circumstances. I bodged together the code a long time ago for a specific use case I had, so many normal uses of Python imports are not properly supported.

Installation

pip install stickytape

Usage

You can tell stickytape which directories to search using the --add-python-path argument. For instance:

stickytape scripts/blah --add-python-path . > /tmp/blah-standalone

Or to output directly to a file:

stickytape scripts/blah --add-python-path . --output-file /tmp/blah-standalone

You can also point stickytape towards a Python binary that it should use sys.path from, for instance the Python binary inside a virtualenv:

stickytape scripts/blah --python-binary _virtualenv/bin/python --output-file /tmp/blah-standalone

Stickytape cannot automatically detect dynamic imports, but you can use --add-python-module to explicitly include modules:

stickytape scripts/blah --add-python-module blah.util

By default, stickytape will ignore the shebang in the script and use "#!/usr/bin/env python" in the output file. To copy the shebang from the original script, use --copy-shebang:

stickytape scripts/blah --copy-shebang --output-file /tmp/blah-standalone

As you might expect with a program that munges source files, there are a few caveats:

  • Due to the way that stickytape generates the output file, your script source file should be encoded using UTF-8. If your script doesn't declare its encoding in its first two lines, then it will be UTF-8 by default as of Python 3.
  • Your script shouldn't have any from __future__ imports.
  • Anything that relies on the specific location of files will probably no longer work. In other words, __file__ probably isn't all that useful.
  • Any files that aren't imported won't be included. Static data that might be part of your project, such as other text files or images, won't be included.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.