Skip to main content

Convert Python packages into a single script

Project description

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.

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-1.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

scriptmerge-1.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: scriptmerge-1.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.5 Linux/6.5.0-17-generic

File hashes

Hashes for scriptmerge-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3c5bc476fdb0dd93563a53caf52f1be2385647c298cb8b99f83db77a6a309102
MD5 0fcfb9452d8ff9b0c4c190331c702ee8
BLAKE2b-256 2bad6a154c68293118fc8ea0ed41d7baeff6d282161e5684a6e44ce3fad6ee6a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: scriptmerge-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.5 Linux/6.5.0-17-generic

File hashes

Hashes for scriptmerge-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1e08c136628d2ba3c0c3d8ed13e36d102e01da72e8c5bcfcb5ef19619cd068b
MD5 3c72d336d84ba3e2befd39ddbd996350
BLAKE2b-256 64b434a0914b1c551c1b83c0c2bb18cb88f476c050baf0bc302a4996cb40e5a1

See more details on using hashes here.

Supported by

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