Skip to main content

pycociC

High-performance, lightweight utility for cleaning Python and Numba bytecode cache files.

(Pronounced: "py-cocky-C(leaner)")

PyPI version Downloads Python versions License GitHub stars


Overview

pycociC traverses directory trees or entire disk drives to safely detect and delete compiled Python bytecode (.pyc, .pyo) and Numba JIT cache files (.nbc, .nbi) located inside __pycache__ directories.

It automatically prunes empty __pycache__ directories after cleaning and reports freed disk space in human-readable units.


Features

  • Recursive Cache Removal: Scans project directories or all mounted system partitions.
  • Broad Cache Coverage: Cleans CPython bytecode (.pyc, .pyo) as well as Numba cache files (.nbc, .nbi).
  • Empty Directory Pruning: Removes leftover empty __pycache__ folders upon file deletion.
  • Human-Readable Metrics: Accurately reports reclaimed space using clear SI units (B, kB, MB, GB, up to YB).
  • Safe Execution: Silently bypasses permission-restricted files and runs in an isolated thread with raised recursion limits to navigate deeply nested directory structures.
  • CLI & Library: Use directly from the terminal (pycocic) or integrate into Python code and CI/CD pipelines.

Installation

Install pycociC using your preferred package manager:

Via pip

pip install pycociC
pipx install pycociC

Via Poetry

poetry add pycociC

CLI Usage

When cleaning bytecode files, run with Python's -B flag or set PYTHONDONTWRITEBYTECODE=1 to prevent Python from recreating new .pyc files while running the cleaner itself:

python -B -m pycociC -d .

Or via the installed console entrypoint:

PYTHONDONTWRITEBYTECODE=1 pycocic -d .

CLI Arguments

usage: pycociC [-h] [-t DIRS [DIRS ...]]

pycociC - A tool to remove pycache (and numba cache) files

options:
  -h, --help            Show this help message and exit
  -t, -d, --dirs DIRS [DIRS ...]
                        Directories to search for pycache files (default: all mounted disks)

Examples

Clean Specific Directories

python -B -m pycociC -d ./src ./tests

Output:

Starting for ./src, ./tests


src/__pycache__
	module.cpython-311.pyc (3kB 210B)
	helpers.cpython-311.pyc (1kB 450B)
src/__pycache__

tests/__pycache__
	test_module.cpython-311.pyc (2kB 890B)
tests/__pycache__


Removed 7kB 550B

Clean All Disks

python -B -m pycociC
Starting for /, /mnt/data


/home/user/projects/api/__pycache__
	main.cpython-310.pyc (4kB 699B)
	config.cpython-310.pyc (1kB 153B)
/home/user/projects/api/__pycache__

...

Removed 42MB 318kB 912B

Python API

You can also use pycociC programmatically in build scripts, cleanup routines, or automation tasks:

from pycociC import eat_cache, bytes_to_pretty_view, dont_write_bytecode

# 1. Clean cache files in specified directories
eat_cache(at_dirs=['./backend', './workers'])

# 2. Check if the current environment suppresses bytecode creation
if not dont_write_bytecode():
    print("Warning: Bytecode generation is currently enabled.")

# 3. Format byte sizes into human-readable strings
print(bytes_to_pretty_view(0))              # '0B'
print(bytes_to_pretty_view(1_000_024))      # '1MB 24B'
print(bytes_to_pretty_view(4_294_967_295))  # '4GB 294MB 967kB 295B'

Available API Functions

Function Description
eat_cache(at_dirs=('.')) Recursively cleans matching cache files from __pycache__ folders under at_dirs and prints summary.
bytes_to_pretty_view(bytes_size, *, skip_zero=False) Converts byte counts to formatted SI representation (B, kB, MB, GB, etc.).
dont_write_bytecode() Returns True if PYTHONDONTWRITEBYTECODE or -B is active, otherwise False.
FP_RE Compiled regular expression matching target cache extensions (`.*.py([co]

How It Works

  1. Target Regular Expression (FP_RE): Matches files ending in .pyc, .pyo, .nbc, and .nbi inside directories named __pycache__.
  2. Drive Auto-Detection: Uses psutil.disk_partitions(all=True) to automatically discover all mounted storage devices when no specific directory argument is provided.
  3. Recursion Safety: Decorator _run_with_max_recursion executes directory traversals in a dedicated thread with increased recursion limits (sys.setrecursionlimit) and custom stack sizes, preventing RecursionError on deeply nested directory trees.

Development & Testing

  1. Clone the repository:

    git clone https://github.com/ndenissov/pycociC.git
    cd pycociC
    
  2. Install dependencies with Poetry:

    poetry install
    
  3. Run the doctest suite:

    poetry run python -m doctest -v pycociC/__init__.py
    

License

Distributed under the MIT License.

Copyright (c) 2022-2026 Nikita Denissov.

Metadata

Release files for pycociC 1.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycociC 1.1.4
File Size Uploaded
pycocic-1.1.4.tar.gz 5.9 kB Details

Built distribution (wheel)

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

Total release size: 13.0 kB

Release files / pycocic-1.1.4.tar.gz

Download URL pycocic-1.1.4.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
62d5938a12a4da0ea5e334fa045f41c927aa3d35c2023f6f1394497ed35e4f0c
BLAKE2b-256 checksum
How to use checksums
8930f7b587291d01810a4ffdffb8c38a20eb6976ff3449d35e1bc1273fa4c47c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.13.15 Linux/7.2.6-zen2

Release files / pycocic-1.1.4-py3-none-any.whl

Download URL pycocic-1.1.4-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
491f7829f5826301a92fb480df81418a3790b7a655b2965f174c1ce806299670
BLAKE2b-256 checksum
How to use checksums
7e6e8a8ca1f092c353f69c77137b10a58892c39a3b937980e7248e4936951aa1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.13.15 Linux/7.2.6-zen2

Release history Release notifications | RSS feed

This release

1.1.4 This release

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

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