Registry / backports-tempfile

backports-tempfile

JSON →
library1.0pypypi✓ verified 23d ago

This package provides backports of new features found in Python's standard `tempfile` module, primarily `tempfile.TemporaryDirectory` (introduced in Python 3.4), allowing its use in older Python environments, including Python 2.7, 3.4, 3.5, and 3.6. It is currently at version 1.0, with its last release in October 2017, indicating a very slow or ceased release cadence given its backport nature and the progression of Python versions.

pip install backports-tempfile
INSTALL
IMPORT
SIG · BACKPORTS-TEMPFILE
B
backports-tempfile
pythonv1.0
Install
1.5s avg
Import
25ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.028s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.022s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TemporaryDirectory
from backports import tempfile # Then use tempfile.TemporaryDirectory
from tempfile import TemporaryDirectory
The original 'tempfile' module on older Python versions (before 3.4) does not have TemporaryDirectory. Using 'from backports.tempfile import TemporaryDirectory' directly might also work but 'from backports import tempfile' is the documented pattern.

Demonstrates how to use `TemporaryDirectory` from the `backports.tempfile` package as a context manager to create and automatically clean up a temporary directory.

from backports import tempfile import os # Create a temporary directory using the backported feature with tempfile.TemporaryDirectory() as temp_dir: print(f"Created temporary directory: {temp_dir}") # Example: create a file inside the temporary directory file_path = os.path.join(temp_dir, "my_temp_file.txt") with open(file_path, "w") as f: f.write("Hello from tempfile backport!") print(f"File created at: {file_path}") print(f"Contents of {file_path}: {open(file_path).read()}") # At this point, temp_dir and its contents should be automatically removed
Debug
Known issues
gotchaThis package primarily backports `TemporaryDirectory` which became standard in Python 3.4. If you are targeting Python 3.4 or newer, this backport is generally not needed, and you should use the native `tempfile` module directly.
fix
For Python 3.4 and later, simply use `from tempfile import TemporaryDirectory`.
affects: <3.4
gotchaThe package has not been updated since October 2017. This means it only backports features up to Python 3.6's `tempfile` module. Newer features introduced in the standard `tempfile` module (e.g., `delete_on_close` for `NamedTemporaryFile` in Python 3.12, or `errors` parameter for `TemporaryFile` in 3.8) are not included.
fix
If you need newer `tempfile` features, you must use a Python version where those features are natively available or seek alternative backports if they exist.
affects: All versions of backports-tempfile
gotchaAs with any backport, using `backports.tempfile` in an environment where the native `tempfile` module is already sufficient can introduce unnecessary dependencies and potential confusion, especially if subtle behavioral differences exist or if code paths are not explicitly managed for different Python versions.
fix
Ensure conditional imports or clear version requirements are in place if using backports in a codebase targeting multiple Python versions. Only install and use if genuinely targeting older Python versions that lack the desired `tempfile` functionality.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'backports.tempfile'
The 'backports-tempfile' package is not installed in the current Python environment or is not accessible on the Python path.
fix
pip install backports-tempfile
Could not find a version that satisfies the requirement backports.tempfile
Pip is unable to locate a suitable version of 'backports-tempfile' to install, often due to searching for stable releases when only pre-releases were available or due to an outdated pip version.
fix
pip install --pre backports.tempfile (for older pip versions or if encountering issues with the main release, otherwise ensure pip is up-to-date: `python -m pip install --upgrade pip`)
AttributeError: module 'tempfile' has no attribute 'TemporaryDirectory'
This error typically occurs when running on Python versions older than 3.4 (where `TemporaryDirectory` was introduced) and attempting to use the built-in `tempfile` module instead of the backported version, or if a local file named `tempfile.py` shadows the standard library module.
fix
Ensure 'backports-tempfile' is installed and import `TemporaryDirectory` from the backports module: `from backports import tempfile` then use `tempfile.TemporaryDirectory()`, or `from backports.tempfile import TemporaryDirectory`. Also, check for and rename any local `tempfile.py` files that might be conflicting.
Upgrade
Version history
1.0latest on PyPI · released Oct 13, 2017
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
backports-tempfile — pip install backports-tempfile · libregistry