-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File left open in sitecustomize when executing it in Python 3 #3067
Comments
Thanks for reporting. Could you show us a simple code example that reproduce this problem? Thanks :-) |
Thank you for looking into this! I narrowed down the cause to these lines: import praw After running this code once (using the reddit api library, praw), all future Executes return the aforementioned error. If I change the code in sitecustomize.py as per the description above, this error no longer happens. It is probably that praw isn't closing something? Either way, the standard for opening a file to guarantee that it is closed is with a 'with' statement, from my reading of current Python docs (I'm converting my code over to this style now). So it seems that changing the code in sitecustomize.py fixes this for libraries like praw. Before I filed this bug, I did a lot of searching and found a few people with the same issue with Spyder running their files, but no solution. I hope this change will fix all of their problems as well. |
Looking at the code extract, looks like your solution is correct. Could you create a pull request with hese modifications ? That said, the file should be closed when the file object is destroyed, which should happen quick because it's a temporary value. |
@ekandrot, don't worry, since this is a simple fix I'll do it myself :-) |
Thanks! I was away this weekend and just saw this. Next time I'll submit a pull request. |
Description of your problem
When I run in Windows10, I get the following error every time I execute file from the UI:
C:\Users\Edward\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py:88: ResourceWarning: unclosed file <_io.BufferedReader name='C:/Users/Edward/test.py'>
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
What steps will reproduce the problem?
What is the expected output? What do you see instead?
No warning with each run - it crowds out my results.
Please provide any additional information below
I fixed it by changing line 88 in the file to:
with open(filename, 'rb') as ff:
exec(compile(ff.read(), filename, 'exec'), namespace)
which will then properly close the file.
Versions and main components
Dependencies
Please go to the menu entry
Help > Optional Dependencies
(orHelp > Dependencies
), press the buttonCopy to clipboard
and paste the contents below:
IPython >=1.0 : 4.1.2 (OK)
jedi >=0.8.1;<0.9.0: 0.9.0 (NOK)
matplotlib >=1.0 : 1.5.1 (OK)
pandas >=0.13.1 : 0.18.0 (OK)
pep8 >=0.6 : 1.7.0 (OK)
pyflakes >=0.6.0 : 1.1.0 (OK)
pygments >=1.6 : 2.1.1 (OK)
pylint >=0.25 : None (NOK)
qtconsole >=4.0 : 4.1.1 (OK)
rope >=0.9.2 : 0.9.4-1 (OK)
sphinx >=0.6.6 : 1.3.1 (OK)
sympy >=0.7.3 : 1.0 (OK)
zmq >=2.1.11 : 15.2.0 (OK)
The text was updated successfully, but these errors were encountered: