Skip to main content

scriptmerge

Convert Python packages into a single script as a standalone .py file or a .pyz file.

Scriptmerge can be used to convert a Python script and any Python modules it depends on into a single-file Python script.

If you want output to be a .py file can use the compilepy command which outputs a .py file. If you want output to be a .pyz file can use the compilepyz command which outputs a .pyz file.

The .pyz is a zipapp .pyz file. It is recommended to use the .pyz file as it is a more modern way to distribute Python applications and it is faster to build and to load.

I also recommend checking out PyInstaller.

Since scriptmerge relies on correctly analyzing both your script and any dependent modules, it may not work correctly in all circumstances.

Documentation

See the documentation for more information.

Installation

pip install scriptmerge

Usage

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

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

Or to output directly to a file:

scriptmerge compilepy 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 compilepyz scripts/blah --python-binary _virtualenv/bin/python --output-file /tmp/blah-standalone

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

scriptmerge compilepyz 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 compilepyz scripts/blah --exclude-python-module blah\.*

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

scriptmerge compilepy 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.

  • When using compilepy 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.

Metadata

Release files for scriptmerge 3.1.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 3.1.0
File Size Uploaded
scriptmerge-3.1.0.tar.gz 15.6 kB Details

Built distribution (wheel)

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

Total release size: 36.0 kB

Release files / scriptmerge-3.1.0.tar.gz

Download URL scriptmerge-3.1.0.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
54f0fa8890f530de0205ade49bd76391528970e9092929d4e5453ce7bd111dcd
BLAKE2b-256 checksum
How to use checksums
1cc959102510ce1a10da6333e7c7bb7feb6e1852b302152712e3609fc476cf1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.2

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

Download URL scriptmerge-3.1.0-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e14ee40c76a166aa20c0b811b018d2facecd50c7cc31a542e39b64a6e4615c5c
BLAKE2b-256 checksum
How to use checksums
d983eeec5b9f45321bc430001ac2c3205a0e533d75e0c2121fe0d4b5908648ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.2

Release history Release notifications | RSS feed

This release

3.1.0 This release

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

1.0.0

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