Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python import doesn't work in VS2012

I'm having trouble using python imports in VS2012. I can't get the project to compile/run when using imports. If I don't have any imports, python will run main ok (and print "hello world"). It seems to be an error with VS, maybe a configuration because when I disable exceptions in VS, it runs fine.

At first it errors saying:

[WinError 2] The system cannot find the file specified

If I continue a few times it then shows:

[WinError 2] The system cannot find the file specified: 'C:\\Users\\Drew Cross\\Documents\\Visual Studio 2012\\Projects\\Test\\Model\\__init__.pyd'

Then it finally runs after a couple more continues.

I have the following directory structure:

main.py
Model
    /__init__.py
    /graph.py

main.py:

import Model.graph

def main():
    print('Hello World')

if __name__ == '__main__':main()

graph.py:

class graph():
    def __init__(self):
        neighbors = []
like image 916
drew212 Avatar asked Sep 23 '26 04:09

drew212


1 Answers

I've seen this when using Python 3.3, which raises new exceptions (such as FileNotFoundError) that Python Tools for Visual Studio doesn't yet correctly suppress.

A temporary workaround is to ignore the following exceptions, which are commonly raised during the import process. You can do so by adding the following Python Exceptions (via Visual Studio's Exceptions dialog) and uncheck "User-unhandled" to ignore them.

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

More discussion can be found in this discussion.

like image 64
Jon-Eric Avatar answered Sep 26 '26 06:09

Jon-Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!