https://github.com/likianta/python-tree-shaking
Aggressive tree shaking based on module graph, to tremendously reduce python dependencies size by 99%.
https://github.com/likianta/python-tree-shaking
python python-packaging tree-shaking
Last synced: 29 days ago
JSON representation
Aggressive tree shaking based on module graph, to tremendously reduce python dependencies size by 99%.
- Host: GitHub
- URL: https://github.com/likianta/python-tree-shaking
- Owner: likianta
- Created: 2024-07-12T11:38:17.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2026-03-06T09:13:25.000Z (about 1 month ago)
- Last Synced: 2026-03-06T13:28:57.463Z (about 1 month ago)
- Topics: python, python-packaging, tree-shaking
- Language: Python
- Homepage:
- Size: 451 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
Awesome Lists containing this project
README
# Python Tree Shaking
[中文版 (最新)](./README.zh.md)
*English README is under construction.*
## Install
```sh
pip install tree-shaking
```
Be noted `tree-shaking` requires Python >= 3.12.
## Usage
Project tree (before):
```sh
# my example project
/workspace/hello-world
|= .venv
|= hello_world
|- __init__.py
|- __main__.py
|- ...
|= dist
|- pyprojet.toml
|- tree_shaking.yaml
|- ...
```
"tree_shaking.yaml" content:
```yaml
root: .
search_paths:
- /.venv/Lib/site-packages
-
entries:
- /hello_world/__main__.py
```
Create a temporary script (e.g. "build.py"), code like this:
```python
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:
```sh
# 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.