12

Googled my way around this issue, but didn't find any solutions. I'm running Python 3.3 with Eclipse and PyDev plugin, and when I run any Python project, I get the following message:

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/update_checker.py:37: ResourceWarning: unclosed file <_io.BufferedReader name='/var/folders/x4/st67yr0x6qg7znh7sdfr94kh0000gn/T/update_checker_cache.pkl'>
  permacache = pickle.load(open(filename, 'rb'))

I'm kind of new to Python, and I have no idea what this means. I wanted to ask before deleting this to make sure it's safe to delete. What does this even mean? I get there's an open file... but why is Python complaining to me? I'm using the PRAW library, if that makes any difference.

Update: My code is here on sourceforge, but it doens't reliably come up with the error (tried on 2 different computers). Once the error comes up, it never goes away.

Mgamerz
  • 2,704
  • 2
  • 21
  • 44
  • It could help to show the code that is actually causing this don't you think? – Loïc Faure-Lacroix Jan 11 '14 at 02:47
  • Well, it's a warning from a third-party python module https://github.com/bboe/update_checker which has little with python itself. I guess you may safely ignore the message. Or you may drop a letter to Mr. Bryce Boe, the author of the module, describing your problem (you may find the e-mail on the corresponding github page). – user3159253 Jan 11 '14 at 02:53
  • if you `open` a file in python, you will be not allowed to delete that file until you `close` it, so just try delete it? – zhangxaochen Jan 11 '14 at 03:45
  • I don't open any files in my script currently. Also, it doens't tell me what's causing the problem in eclipse, only that the message comes up. It gives no indication where the problem is occuring from. My code is on sourceforge (earthporn-downloader), but it's not working right now (the one on sourceforge, my local one does) – Mgamerz Jan 11 '14 at 06:33

1 Answers1

20

This ResourceWarning means that you opened a file, used it, but then forgot to close the file. Python closes it for you when it notices that the file object is dead, but this only occurs after some unknown time has elapsed. Thus in recent versions, Python also prints a ResourceWarning when it does that. It is a way for you to quickly identify where the unclosed files are, and properly close them. It might be important on some platforms which cannot have more than N files opened at the same time (e.g. 1024). Also, specifically on Windows, you cannot do some operations with a file if it's still open (e.g. deleting it).

In this case, the line in the file update_checker.py needs to be fixed to say:

with open(filename, 'rb') as f:   # will close() when we leave this block
    permacache = pickle.load(f)
Armin Rigo
  • 10,710
  • 28
  • 44
  • 1
    Python 3.3 normally doesn't print that warning by default, unless running a debug build of python, or started it with an `-W` option that enables warnings such as `-Wd`, or while running unit tests with the `unittest` module. Perhaps `Eclipse` or the `PyDev` plug-in turned on the warning? – Mark Tolonen Jan 11 '14 at 10:31
  • I'll see if I can send this to the developers. It appears to be something to do with PIP, which is how I installed PRAW, and it comes up for everything now. – Mgamerz Jan 11 '14 at 18:57
  • same situation, same warning. python 3.3, pyvenv-3.3, praw installed with pip, same error. – here Jan 18 '14 at 22:59
  • I contacted the developer. I made a pull request with the fix by Armin Rigo. He merged it, but I'm not sure if he's updated the praw version available through pip. To avoid this issue, just put the fix in the update_checker.py file in the site-packages folder of your python distribution. Didn't cause any issues for me. – Mgamerz Jan 19 '14 at 00:29