Skip to main content

Convert Python packages into a single script

Project description

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.

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

scriptmerge-3.1.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scriptmerge-3.1.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file scriptmerge-3.1.0.tar.gz.

File metadata

  • Download URL: scriptmerge-3.1.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for scriptmerge-3.1.0.tar.gz
Algorithm Hash digest
SHA256 54f0fa8890f530de0205ade49bd76391528970e9092929d4e5453ce7bd111dcd
MD5 81ee146b40f9b9d95ea002869cbc548d
BLAKE2b-256 1cc959102510ce1a10da6333e7c7bb7feb6e1852b302152712e3609fc476cf1d

See more details on using hashes here.

File details

Details for the file scriptmerge-3.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for scriptmerge-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e14ee40c76a166aa20c0b811b018d2facecd50c7cc31a542e39b64a6e4615c5c
MD5 4e0dc1c6d29e4acaa8f3e5d798d89d15
BLAKE2b-256 d983eeec5b9f45321bc430001ac2c3205a0e533d75e0c2121fe0d4b5908648ec

See more details on using hashes here.

Supported by

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