commoncode Utilities
commoncode is a collection of essential utilities for Python, originally split from the ScanCode project. It provides robust tools for file system operations, hashing, system information, command execution, and more. It is actively maintained with frequent minor releases to support new Python versions and address bug fixes. The current version is 32.4.2.
Common errors
-
ModuleNotFoundError: No module named 'distutils.version'
cause Using commoncode with Python 3.12 or newer, which removed the `distutils` package.fixEnsure you are using a commoncode version compatible with Python 3.12+. commoncode 32.4.1 (and newer) includes updates for Python 3.14 (and implicitly 3.12/3.13). -
TypeError: 'Argument' object is not callable
cause Using an older commoncode version (<32.3.0) with a newer 'click' library (8.1.3+) due to API changes in 'click'.fixUpgrade commoncode to version 32.3.0 or higher: `pip install --upgrade commoncode`. -
UnicodeEncodeError: 'utf-8' codec can't encode character...
cause Attempting to process file paths containing characters that cannot be encoded with the default system encoding (or UTF-8) with commoncode versions prior to 32.4.0.fixUpgrade to commoncode 32.4.0 or newer: `pip install --upgrade commoncode`. This version includes fixes for handling non-UTF-8 byte paths.
Warnings
- breaking commoncode dropped support for Python 3.9 in version 32.4.0. Using commoncode 32.4.0 or newer with Python 3.9 will lead to installation errors or runtime issues.
- breaking Versions of commoncode prior to 32.3.0 had compatibility issues with 'click' version 8.1.3 and newer, potentially causing errors in CLI applications that use commoncode.
- gotcha Paths containing non-UTF-8 bytes were not handled correctly in versions prior to 32.4.0, potentially leading to errors when processing file paths with unusual characters.
- breaking Version 32.2.0 introduced adjustments to the `as_unicode` function due to breaking changes in `bs4` (BeautifulSoup4) version 4.13. If you rely on `as_unicode` and use an older `commoncode` with `bs4>=4.13`, you may encounter errors.
Install
-
pip install commoncode
Imports
- file_sha1
import commoncode.fileutils.file_sha1
from commoncode.fileutils import file_sha1
- get_system_info
from commoncode.system import get_system_info
- execute
from commoncode.command import execute
Quickstart
import os
from pathlib import Path
from commoncode.fileutils import file_sha1
# Create a dummy file for demonstration
dummy_file_path = Path('dummy_file.txt')
with open(dummy_file_path, 'w') as f:
f.write('Hello, commoncode!\n')
f.write('This is a test file.\n')
try:
# Get SHA1 checksum of the file
sha1_checksum = file_sha1(str(dummy_file_path))
print(f"SHA1 of '{dummy_file_path}': {sha1_checksum}")
# Get system information
from commoncode.system import get_system_info
system_info = get_system_info()
print(f"Python Version: {system_info.get('python_version')}")
print(f"OS Release: {system_info.get('os_release_id')}")
finally:
# Clean up the dummy file
if dummy_file_path.exists():
os.remove(dummy_file_path)