FileNotFoundError Python 3.3

Oct 10, 2012 at 5:56 PM

I'm having trouble using Python 3.3 with PTVS 1.5 (with Visual Studio 2012).

When I try and debug using the Python 3.3 interpreter, I immediately get a FileNotFoundError ([WinError 2] The system cannot find the file specified).

I can Continue through dozens of such exception and eventually the program does appear to run, but none of the debugging facilities (such as the Call Stack window) appear to work.

I've tried both the 32-bit and the 64-bit versions of Python 3.3, both cause the same error.

However, the following work successfully for me:

  • Python 64-bit 2.7
  • Python 64-bit 3.2

Any ideas what I need to do to get Python 3.3 to work as well?

Thank you!

Coordinator
Oct 11, 2012 at 12:30 AM

Hi, thanks for the report. We're having some trouble reproducing this, so we were wondering if you could provide some more information:

  1. Do you have Just My Code enabled (on the Tools->Options->Debugging page)?
  2. You said the Call Stack window does not work, but if you can find any call stacks or tracebacks at any time these would be incredibly helpful.
  3. Attaching the contents of the "Tools->Python Tools->Diagnostics Info" window as a text file will help us see if there's something unusual or unexpected about your configuration - it will contain full paths, so feel free to find/replace "x:\Users\<your name>" with something else if you want to hide it.
  4. Did you install Python 3.3 for all users or just yourself? (We have a known issue in the RC when installing 64-bit versions of CPython for the current user only, which will be fixed in the final release.)

Also, if you can reproduce this on a significant project but not a blank (Hello World) template, that's useful information. If possible, we'd want to be able to test with a copy of your code, but if not then giving us some idea of the nature of your project (libraries used, sort of thing) will probably do.

Oct 11, 2012 at 4:40 PM
Thank you for your help!
I've narrowed the problem down more but first, to answer your questions:
  1. Yes, I have Just My Code enabled. (Disabling it didn't help.)
  2. I can't find any call stacks or tracebacks. (Any idea where to look?)
  3. I don't see an "attach" button on the toolbar, did you mean for me to paste in the contents as text?
  4. I installed Python 3.3 for all users.

The root of the problem is that when debugging in PTVS using Python 3.3, failed imports incorrectly raise FileNotFoundError instead of ImportError. (The standard library often attempts an import with a except clause to catch only ImportError.)

The simplest way to reproduce the problem is to debug a single-line file of "import blah". Using Python 3.2, it correctly raises ImportError and the debugger breaks on the import line (and the call stack is correct). Using Python 3.3, it incorrectly raises FileNotFoundError and the debugger is unable to find the offending line or show a call stack.

Executing the same "import blah" using Python 3.3 from the command line correctly raises ImportError.

Is there perhaps some type of import hook in PTVS that might be incorrectly causing the FileNotFoundError?

Coordinator
Oct 11, 2012 at 5:12 PM

Okay, so the issue is that our debugger doesn't recognize the FileNotFoundError (since it's new and we haven't added it to the list of exceptions yet), doesn't realize that it is handled and so breaks. The exception itself is correct, but it is handled somewhere within CPython that we can't analyse.

An immediate solution is to open the Exceptions dialog, choose "Add", select "Python Exceptions", enter "exceptions.FileNotFoundError" and then deselect the "User-unhandled" checkbox. This isn't perfect, since now all FileNotFoundErrors will bring down your program rather than breaking into the debugger, but it will at least save the trouble of those errors. We'll look at how we can handle this better, hopefully for our final 1.5 release.

(And you're right, there's no attach functionality in the discussions, so that wasn't going to work so well. They tend to be extremely long files, so pasting the text in is not ideal, so we may just provide an email address in the future. Since we've figured this one out, though, there's no need to worry about it now.)

Oct 11, 2012 at 6:47 PM

Ah, that makes sense. It's normal for a FileNotFoundError to be thrown and caught and cause an ImportError the issue was just that the PTVS was prematurely breaking on the FileNotFoundError.

I added and ignored three exceptions, and now I'm able to debug without issue.

For reference these are the exceptions I needed to add and deselect the "User-unhandled" checkbox to get my specific project to run:

  • exceptions.FileNotFoundError
  • exceptions.PermissionError
  • zipimport.ZipImportError

The last one is a little curious because it isn't new to Python 3.3. The debugger doesn't break on it when using Python 3.2, but perhaps there's just some code-path change in Python 3.3 that accounts for that difference.

Thank you for your help! And thank you for PTVS in general, it's awesome!

Oct 11, 2012 at 8:44 PM

Just FYI, I don't think this is strictly about new exception types that didn't exist before, I think it really has more to do with some change to the import mechanics (which did change in Python 3.3).

After ignoring those three exceptions, the debugger mostly works correctly, however I still sometimes encounter stray exceptions on start-up that should be ignored. For example, I just got "Import Error: byte code is stale". I suspect this is part of the normal import process (to signal that the pyc file needs to be rebuilt), but using Python 3.3 causes a dialog for such "eventually-caught" exceptions.

Coordinator
Oct 11, 2012 at 10:40 PM

Thanks for the extra info.

Our fix for this will involve suppressing all errors while importing, rather than ignoring individual ones. We're still reviewing the changes internally, but I did post some code at http://pytools.codeplex.com/workitem/850 that you should be able to merge into visualstudio_py_debugger.py manually to get a more robust fix sooner. (One of the great benefits of using Python for our debugger is we can do hotfixes like this easily.)