The zipimport
module is automatically used by the standard import to handle .zip
sys.path elements.
Is it possible to add hooks to support other file types? for example a handler for .tar.gz
?
so for example, if sys.path contains /path/to/archive.tar.gz
or /path/to/archive.xyz
handlers can be provided to open and read .tar.gz
or .xyz
files.
Yes, there are two ways of doing this:
__builtin__.__import__()
function with your own custom implementation. This is a low-level way of completely overriding what the import
keyword does, and is not recommended for general use.sys.meta_path
which implements the desired functionality, or add a callable which returns such a finder to sys.path_hooks
. Finders are easier to implement in Python 3 than in Python 2, because 3.x provides a lot of building blocks in importlib
. However, they can be implemented in Python 2 as well (you just have to write more code).In general, (2) is much easier than (1) regardless of 2.x vs 3.x. (1) is only recommended as a last resort. For more on (2), see PEP 302.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With