patool is a portable archive file manager that supports a wide array of formats like 7z, RAR, ZIP, and TAR. It simplifies archive handling by abstracting away the complexities of various underlying compression programs. While it can natively handle formats like TAR, ZIP, BZIP2, LZMA, and GZIP, it often relies on helper applications installed on the system for other formats. The library is actively maintained, with frequent releases, and is currently at version 4.0.4, requiring Python 3.11 or later.
pip install patoolVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create and extract a ZIP archive programmatically using `patoolib`. It first creates a dummy text file, archives it into a .zip file, then extracts the contents to a new directory, and finally cleans up the created files and folders. Ensure you have the necessary system-level archiving tools (like `zip`/`unzip`) installed if you use formats not natively supported by patool.
Review code that might implicitly rely on `patool` failing when the primary program doesn't support a compression type. Consider explicitly specifying the `program` argument in `patoolib` functions if specific external tools are desired.
Install the appropriate command-line utilities for the archive formats you intend to use. For example, on Linux, `sudo apt-get install unrar p7zip-full xz-utils`. On Windows, you might need to manually install these tools and add their executable directories to your system's PATH environment variable.
Always use `import patoolib` to access the library's functions.
For non-interactive environments, set the `interactive=False` parameter in `patoolib` function calls (e.g., `patoolib.extract_archive('archive.rar', outdir='/tmp', interactive=False)`) or use the `--non-interactive` global option for the command-line tool. You can also provide passwords via the `password` argument if applicable.Ensure your Python environment meets the minimum `3.11` requirement. If you encounter issues with specific formats or wish to leverage the latest features, consider using Python 3.14 or newer.
Install the required helper application for the specific archive format (e.g., `sudo apt-get install unrar` for RAR files on Linux, or install 7-Zip/WinRAR on Windows and ensure they are in your system's PATH).
Install the `patool` library using pip: `pip install patool`. If using a virtual environment, ensure it's activated.
Ensure the correct Python interpreter is being used and that `patool` is installed within that environment (`which python` and `pip list` can help). If running in a specific context (like an IDE or a build system), verify its Python environment settings. Reinstalling in a fresh virtual environment often resolves conflicts.
Increase patool's verbosity (`verbosity=1` or `verbosity=2` in `extract_archive`) to get more detailed output from the helper application, which might reveal the specific cause of the failure. Verify the archive's integrity and ensure the helper application works correctly when run manually with the same command.