Skip to main content

scriptmerge: Convert Python packages into a single script

Scriptmerge 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 scriptmerge relies on correctly analysing both your script and any dependent modules, it may not work correctly in all circumstances.

Installation

pip install scriptmerge

Usage

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

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

Or to output directly to a file:

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

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

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

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

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

Scriptmerge can exclucde modules from be added to output. This is useful in special cases where is it known that a module is not required to run the methods being used in the output. An example might be a script that is being used as a LibreOffice macro. You can use --exclude-python-module to explicitly exclude modules.

--exclude-python-module takes one or more regular expressions

In this example module blah is excluded entirly. blah\.* matches modules such as blah.__init__, blah.my_sub_module.

scriptmerge scripts/blah --exclude-python-module blah\.*

By default, scriptmerge 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:

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

Scritpmerge can strip all doc strings and comments from imported modules using the --clean option.

scriptmerge --clean

To see all scriptmerge options:

scriptmerge --help

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

  • Due to the way that scriptmerge 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.

Credits

Scriptmerge is a fork of stickytape.

Credit goes to Michael Williamson as the original author.

Release files for scriptmerge 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scriptmerge 1.0.0
File Size Uploaded
scriptmerge-1.0.0.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scriptmerge 1.0.0
File Interpreter ABI Platform
scriptmerge-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / scriptmerge-1.0.0.tar.gz

Download URL scriptmerge-1.0.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b97dd3c61a3547fad7189c3793905a359fb997507d1e4af87a9c16001897b0ed
BLAKE2b-256 checksum
How to use checksums
90719ff6d7318978dae48cfa0b6534189ff8018ee9c37a4a20e25be48825f354
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / scriptmerge-1.0.0-py3-none-any.whl

Download URL scriptmerge-1.0.0-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2bab6c3b53b8aa0fd95fdb404ff8e33b94dbeb11b7f6d7568102f3bbbd78688b
BLAKE2b-256 checksum
How to use checksums
28d18c23424e06e687b6a3b136b55de1eefe0d7066c71ea8f4163fc3c9c47db8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

3.1.0

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.1.0

2 release files

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page