Registry / serialization / py7zr
library1.1.3pypypi✓ verified 27d ago

Py7zr is a pure Python library for working with 7-Zip archives, enabling creation, extraction, and inspection of `.7z` files without requiring external binaries. The library is actively maintained with a consistent release cadence, recently moving to version 1.x, and supports modern Python versions.

pip install py7zr
INSTALL
IMPORT
SIG · PY7ZR
P
py7zr
serializationpythonv1.1.3
Install
3.2s avg
Import
245ms
Disk
34MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.3 · 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.248s · 34MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.2s · import 0.242s · 36MB
34MB installed
● package 34MB
Code
Verified usage

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

SevenZipFile
from py7zr import SevenZipFile
is_7zfile
from py7zr import is_7zfile

This quickstart demonstrates how to create a 7z archive with a single file and then extract its contents to a new directory. It includes basic error handling and cleanup.

import py7zr import os archive_name = "example.7z" file_to_archive = "test_file.txt" # Create a dummy file for archiving with open(file_to_archive, "w") as f: f.write("This is a test file for py7zr.\n") try: # Create a 7z archive with py7zr.SevenZipFile(archive_name, 'w') as archive: archive.write(file_to_archive, arcname='renamed_test_file.txt') print(f"Archive '{archive_name}' created with '{file_to_archive}'.") # Extract from the 7z archive extract_dir = "extracted_files" os.makedirs(extract_dir, exist_ok=True) with py7zr.SevenZipFile(archive_name, mode='r') as archive: archive.extractall(path=extract_dir) print(f"Files extracted to '{extract_dir}'.") # Verify extraction extracted_path = os.path.join(extract_dir, 'renamed_test_file.txt') if os.path.exists(extracted_path): with open(extracted_path, 'r') as f: content = f.read() print(f"Content of extracted file: {content.strip()}") else: print("Extraction verification failed.") except Exception as e: print(f"An error occurred: {e}") finally: # Clean up created files and directories if os.path.exists(file_to_archive): os.remove(file_to_archive) if os.path.exists(archive_name): os.remove(archive_name) if os.path.exists(extract_dir): for root, dirs, files in os.walk(extract_dir, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) os.rmdir(extract_dir) print("Cleanup complete.")
Debug
Known issues
breakingVersions of py7zr from 1.0.0 onwards require Python 3.10 or higher. Earlier versions supported Python 3.7+.
fix
Ensure your environment uses Python 3.10+ or pin py7zr to a version below 1.0.0 (e.g., `py7zr<1.0.0`).
affects: <1.0.0
breakingThe `getInfo()` method of `SevenZipFile` now returns `FileInfo` dataclass objects instead of older tuple or dictionary structures. Code expecting direct dictionary/tuple access will break.
fix
Update your code to access file information via attributes of the `FileInfo` object (e.g., `info.name`, `info.size`).
affects: >=1.1.0
gotchaPrior to version 1.1.0, using `SevenZipFile` with `IO[bytes]` stream objects for reading or writing archives might have been inconsistent or caused issues. This was explicitly fixed in v1.1.0.
fix
For robust handling of byte streams, upgrade to py7zr version 1.1.0 or newer. If on older versions, ensure file-like objects are properly buffered or use file paths where possible.
affects: <1.1.0
gotchaIn versions prior to 0.21.1, the `unpack_7zarchive` function might not have reliably closed the archive handle, potentially leading to resource leaks. This was addressed in v0.21.1.
fix
Upgrade to py7zr version 0.21.1 or newer to ensure proper resource management when using `unpack_7zarchive`. Alternatively, use `SevenZipFile` with a `with` statement for explicit context management.
affects: <0.21.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'py7zr'
The `py7zr` library is not installed in the current Python environment, or the Python environment where it's installed is not active.
fix
Install the library using pip: `pip install py7zr`. If you are using a virtual environment, ensure it is activated before running the installation command.
py7zr.Bad7zFile: not a 7z file
The file being opened is either not a valid 7-Zip archive, is corrupted, or has an incorrect file extension and `py7zr` cannot recognize its format.
fix
Ensure the file is a legitimate and uncorrupted `.7z` archive. You can use `py7zr.is_7zfile('your_file.7z')` to pre-check if a file is a valid 7z archive before attempting to open it.
py7zr.PasswordRequired
An attempt was made to extract an encrypted 7-Zip archive without providing the correct password, or an incorrect password was supplied.
fix
Provide the correct password when initializing the `SevenZipFile` object: `with py7zr.SevenZipFile('archive.7z', mode='r', password='your_password') as archive:`.
KeyError: 'filename_not_found'
This error occurs when trying to get information about or extract a file (e.g., using `getinfo()` or `extract()`) that does not exist within the 7-Zip archive under the specified name or path.
fix
Verify the exact filename and its path within the archive. You can list all member names using `archive.getnames()` to ensure the target file exists and its name is correctly spelled.
TypeError: argument of type 'str' is not iterable
The `targets` argument in `extract()` or `extractall()` methods expects an iterable (like a list or set) of filenames, but a single string was provided directly.
fix
Wrap the single filename in a list, even if extracting only one file: `archive.extract(targets=['single_file.txt'])`.
Upgrade
Version history
1.1.3latest on PyPI · released Jun 19, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
8
Amazon
2
Resources
py7zr — pip install py7zr · libregistry