PTVS icon indicating copy to clipboard operation
PTVS copied to clipboard

Unexpected error in python development: unresolved import '__main__'

Open vsfeedback opened this issue 2 years ago • 3 comments

This issue has been moved from a ticket on Developer Community.


When developing a modular python code (with actual custom modules imported) you can use import __main__ from a module to extract data from main.

The __main__ keyword should not be interpreted by Visual Studio as a module name

That's not what it's actually happening: it's showing a warning in the "Error list" view which says: unresolved import '__main__'


Original Comments

Feedback Bot on 4/13/2022, 10:57 PM:

(private comment, text removed)

Bill Schnurr [MSFT] on 4/15/2022, 00:03 PM:

(private comment, text removed)

Dario Panico on 4/19/2022, 00:18 AM:

(private comment, text removed)

Bill Schnurr [MSFT] on 4/20/2022, 11:06 AM:

(private comment, text removed)

Dario Panico on 4/21/2022, 00:36 AM:

(private comment, text removed)

Bill Schnurr [MSFT] on 4/21/2022, 02:43 PM:

(private comment, text removed)

Bill Schnurr [MSFT] on 4/21/2022, 03:04 PM:

(private comment, text removed)


Original Solutions

(no solutions)

vsfeedback avatar Apr 25 '22 17:04 vsfeedback

@dariopnc opened it originally: I'm here for further needs

dariopnc avatar Jun 06 '22 11:06 dariopnc

Here's a minimal sln example that triggers the wrong behaviour

screenshot

dariopnc avatar Jun 06 '22 12:06 dariopnc

Note that __main__ is not a keyword - it is, indeed, a module name, it just happens to be the name used by Python itself for the main module when registering it in sys.modules.

Either way, code completion should probably consider it "always present", but resolving members might be trickier. When there's a project, we know the startup file, so we could map it to that.

int19h avatar Jun 20 '22 07:06 int19h