[Import-SIG] Loading Resources From a Python Module/Package

Barry Warsaw barry at python.org
Sat Jan 31 18:44:51 CET 2015


On Jan 30, 2015, at 07:52 PM, Donald Stufft wrote:

>> On Jan 30, 2015, at 7:18 PM, Paul Moore <p.f.moore at gmail.com> wrote:
>> Related question - how would the temp files be cleaned up? At exit?
>
>My patch registers an atexit handler that cleans up the temporary files yea.

Why not implement it as a context manager?

I'm not a big fan of overloading the atexit handler because there are
situations where it might not get called (e.g. the program crashes or is kill
-9'd), but a context manager allows the resource to be cleaned up asap.

Reviewing my own uses of pkg_resources.resource_filename() I think it would
work just fine because I rarely need the path much longer than the immediate
operation.  If I did need to cache it more permanently, I could easily do:

    with resource_filename('my.package.path', 'foo.dat') as path:
        shutil.copy(path, some_more_permanent_location)

Easy peasy.

Cheers,
-Barry


More information about the Import-SIG mailing list