Skip to main content

Python tree-shaking for dependencies to reduce the size of packaging.

Project description

Python Tree Shaking

中文版

English README is under construction.

Install

pip install tree-shaking

Be noted tree-shaking requires Python >= 3.12.

Usage

Project tree (before):

# my example project
/workspace/hello-world
|= .venv
|= hello_world
   |- __init__.py
   |- __main__.py
   |- ...
|= dist
|- pyprojet.toml
|- tree_shaking.yaml
|- ...

"tree_shaking.yaml" content:

root: .
search_paths:
  - <root>/.venv/Lib/site-packages
  - <root>
entries:
  - <root>/hello_world/__main__.py

Create a temporary script (e.g. "build.py"), code like this:

import tree_shaking
tree_shaking.build_graph_modules("./tree_shaking.yaml")
tree_shaking.dump_tree("./tree_shaking.yaml", "./dist/minified_libs")

After running, the project tree changes:

# my example project
/workspace/hello-world
|= .venv
|= hello_world
   |- __init__.py
   |- __main__.py
   |- ...
|= dist
   |= minified_libs  # updated
      |= ...
|- pyprojet.toml
|- tree_shaking.yaml
|- ...

You can temporarily exclude ".venv/Lib/site-packages", and add "dist/minified_libs" to Python's sys.path (put it at the first place) to test if worked.

After testing, compress "dist/minified_libs" to zip, and compare its size with ".venv/Lib/site-packages" -- the more heavy dependencies you have, the more notable changes on size reduction.

Incremental Updates

Just rerun "build.py", outputs results to the same path as last time, tree-shaking will find the changed parts and do only necessary adding/deleting operations to the target directory.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

tree_shaking-0.1.0-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file tree_shaking-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tree_shaking-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.1 Windows/11

File hashes

Hashes for tree_shaking-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00f1a22284ab065fe53f83b181c799c4eda9dceb6513537335056d0f0526cb4e
MD5 efd82385ea9ed16c5cd4b6197f7d1546
BLAKE2b-256 0d12e0c7c0141458a49bf7cf019314745031b6c8901312f34a4196d1e8fa1995

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