Backports Tarfile

raw JSON →
1.2.0 verified Tue May 12 auth: no python install: verified quickstart: verified

backports.tarfile provides a backport of the CPython standard library's `tarfile` module, allowing users on older Python versions (>=3.8) to access newer features and bug fixes related to tar archive handling. The current version is 1.2.0, released recently, with several updates in Spring 2024.

pip install backports-tarfile
error ImportError: cannot import name 'tarfile' from 'backports'
cause This error often occurs due to conflicts with the `setuptools` library or issues with Python's namespace package handling, where an 'empty' `backports` package might be shadowing the intended `backports.tarfile` module, especially on Python versions where `setuptools` vendors parts of `backports`.
fix
Ensure backports.tarfile is explicitly installed and potentially try uninstalling and reinstalling it (pip uninstall backports.tarfile && pip install backports.tarfile>=1.2.0). If setuptools is involved, upgrading setuptools or ensuring its dependencies are correctly handled might resolve the conflict. Sometimes, removing a conflicting 'empty' backports namespace package (a directory containing only an __init__.py file) can also help.
error ModuleNotFoundError: No module named 'backports'
cause This means the `backports` namespace package, which `backports.tarfile` resides under, is not installed or discoverable in your Python environment.
fix
Install the backports.tarfile package using pip: pip install backports-tarfile.
error AttributeError: module 'tarfile' has no attribute 'data_filter'
cause This error arises when trying to use newer features of the `tarfile` module, such as `data_filter`, which were introduced in later Python versions (e.g., Python 3.12+), on an older Python environment where `backports.tarfile` is either not correctly replacing the standard library module or the Python version is too old to support even the backported feature.
fix
Ensure backports.tarfile is installed and that your code correctly imports and uses the backported module. If you're explicitly importing tarfile directly, ensure that the backports.tarfile is taking precedence or is directly imported (e.g., import backports.tarfile as tarfile). If the issue persists, verify your Python version meets the minimum requirements for the specific feature you are trying to backport, or consider upgrading your Python environment if the feature is not supported by backports.tarfile on your current version. If using pip, ensure it's up-to-date (pip install --upgrade pip).
gotcha ImportError due to namespace package conflicts, particularly with `setuptools` versions (e.g., 71.0.0+). Users might encounter `ImportError: cannot import name 'tarfile' from 'backports'`.
fix Ensure `backports-tarfile` is explicitly installed. Sometimes, temporarily downgrading `setuptools` or using conditional `pip install backports.tarfile` can resolve it.
gotcha Security Vulnerabilities in Backported `tarfile` Extraction Filtering (pre-Python 3.12). The backported tarfile extraction filtering feature, intended for security, was incomplete and could introduce new security issues if projects switched from custom inspection to relying solely on these filters.
fix Exercise caution when using extraction filtering on older Python versions. Always refer to CVE records for the most up-to-date security information and consider additional validation.
gotcha Misunderstanding its purpose: This library is a backport. If your Python version natively includes the `tarfile` features you need, you should use the standard library's `tarfile` directly, rather than the backport, to avoid unnecessary dependencies or potential conflicts.
fix Only install and use `backports.tarfile` if your Python runtime explicitly lacks the `tarfile` features you require and the backport provides them (e.g., specific compression algorithms or filters only available in newer Python stdlib versions). Use conditional imports (e.g., `if sys.version_info < (3, 14): from backports.zstd import tarfile`) if targeting a range of Python versions.
gotcha Python 3.14 will, by default, filter extracted tar archives and reject files or modify their metadata. This DeprecationWarning appears when `tarfile.extractall()` or `tar.extract()` is called without explicitly providing the `filter` argument.
fix Explicitly use the `filter` argument (e.g., `filter='data'`, `filter='fully_trusted'`, or `filter='tar'`) when extracting tar archives to control this behavior and suppress the deprecation warning. This ensures forward compatibility and consistent behavior with Python 3.14 and later versions.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.02s 18.0M
3.10 alpine (musl) - - 0.02s 18.0M
3.10 slim (glibc) wheel 1.5s 0.01s 18M
3.10 slim (glibc) - - 0.01s 18M
3.11 alpine (musl) wheel - 0.04s 19.9M
3.11 alpine (musl) - - 0.04s 19.9M
3.11 slim (glibc) wheel 1.6s 0.03s 20M
3.11 slim (glibc) - - 0.03s 20M
3.12 alpine (musl) wheel - 0.03s 11.8M
3.12 alpine (musl) - - 0.03s 11.8M
3.12 slim (glibc) wheel 1.4s 0.03s 12M
3.12 slim (glibc) - - 0.03s 12M
3.13 alpine (musl) wheel - 0.02s 11.5M
3.13 alpine (musl) - - 0.03s 11.4M
3.13 slim (glibc) wheel 1.4s 0.02s 12M
3.13 slim (glibc) - - 0.03s 12M
3.9 alpine (musl) wheel - 0.02s 17.5M
3.9 alpine (musl) - - 0.03s 17.5M
3.9 slim (glibc) wheel 1.8s 0.02s 18M
3.9 slim (glibc) - - 0.02s 18M

This example demonstrates how to create and extract a gzipped tar archive using the backported `tarfile` module. The API is identical to the standard library's `tarfile`.

import os
from backports import tarfile

# Create a dummy file
with open('example.txt', 'w') as f:
    f.write('Hello, backported tarfile!')

# Create a tar.gz archive
with tarfile.open('example.tar.gz', 'w:gz') as tar:
    tar.add('example.txt')

print('Archive created: example.tar.gz')

# Extract the archive
with tarfile.open('example.tar.gz', 'r:gz') as tar:
    tar.extractall(path='./extracted_archive')

print('Archive extracted to ./extracted_archive')

# Clean up
os.remove('example.txt')
os.remove('example.tar.gz')
os.remove('./extracted_archive/example.txt')
os.rmdir('./extracted_archive')