Skip to content
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

reportMissingModuleSource: Visual Studio 2022 / Python #7393

Closed
vsfeedback opened this issue Feb 9, 2023 · 27 comments
Closed

reportMissingModuleSource: Visual Studio 2022 / Python #7393

vsfeedback opened this issue Feb 9, 2023 · 27 comments
Assignees

Comments

@vsfeedback
Copy link

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


Hi,

I have a problem with Visual Studio 2022 / Python. I am using Visual Studio to write and debug Python code.

I am not sure if this is a Visual Studio problem or a Python problem or both.

Note that this problem affects both edit mode in Visual Studio and run mode in Visual Studio. In other words, the same errors show up when a Python program is running (using F5 in Visual Studio).

The problem has an easy description, but is rather serious.

I use ‘from HDLmXXX import *’ statements, at the start of some of my Python programs (see 20230130_095849.jpg). That is pretty standard.
20230130_095849.jpg

20230130_095914.jpg

However, the import classes (some of them) are unknown later. See 20230130_095914.jpg.

Why do I get this error? What can I do about this?


Original Comments

Feedback Bot on 1/30/2023, 06:14 PM:

(private comment, text removed)

pschaeffer on 2/2/2023, 05:59 PM:

(private comment, text removed)


Original Solutions

(no solutions)

@judej judej added the needs investigation Could be an issue - needs investigation label Feb 9, 2023
@judej judej changed the title Visual Studio 2022 / Python reportMissingModuleSource: Visual Studio 2022 / Python Feb 9, 2023
@StellaHuang95
Copy link
Contributor

Seems like a duplicate of #7068 and #6713

@StellaHuang95
Copy link
Contributor

More from the original post:

I worked on this a bit further. It turns out to be a circular import problem (see below). The possible bug is that I never got an importError or any other exception. Instead, I got a runtime exception (NameError: name ‘IDLmAJAX’ is not defined). This problem can be easily (not really) reproduced be creating 3 (yes 3) files.
IDLmBuild.py

from IDLmAJAX import *
from IDLmUtility import *
IDLmUtility.getPerceptualHash(‘a’)

IDLmUtility.py

from IDLmAJAX import *
class IDLmUtility(object):
@staticmethod
def getPerceptualHash(urlStr):
print(IDLmAJAX)
IDLmAJAX.py

from IDLmUtility import *
class IDLmAJAX(object):
def runAJAX():
pass

Note that IDLmBuild.py is the ‘main’ program where execution should start. See below.

C:\Users\Peter\Documents\Visual Studio 2022\projects\PythonApps\PythonApps>py IDLmBuild.py
Traceback (most recent call last):
File “C:\Users\Peter\Documents\Visual Studio 2022\projects\PythonApps\PythonApps\IDLmBuild.py”, line 3, in
IDLmUtility.getPerceptualHash(‘a’)
File “C:\Users\Peter\Documents\Visual Studio 2022\projects\PythonApps\PythonApps\IDLmUtility.py”, line 5, in getPercep
print(IDLmAJAX)
NameError: name ‘IDLmAJAX’ is not defined

@StellaHuang95 StellaHuang95 self-assigned this Feb 24, 2023
@StellaHuang95
Copy link
Contributor

Hi there, could you change from IDLmAJAX import * to import IDLmAJAX? That should solve your issue. I believe the former one only creates references to all things defined in module IDLmAJAX, but the module itself is not defined, so it won't work. Please re-open the ticket if the issue persists. Thanks.

@pschaeffer
Copy link

Hi StellaHuang95,

I made the changes you suggested (and some more) and the problem still exists. The current files are provided below.

IDLmAJAX.py

import IDLmUtility
class IDLmAJAX(object):
def runAJAX():
pass

IDLmBuild.py

import IDLmAJAX
import IDLmUtility
IDLmUtility.g()

IDLmUtility.py

import IDLmAJAX
class IDLmUtility(object):
@staticmethod
def g():
print(IDLmAJAX)

When I run IDLmBuild I get "module 'IDLmUtility' has no attribute 'g'"

Of course, that is not true. Worse, Python never detects/reports the circular imports.

Thank you

Peter Schaeffer

@StellaHuang95 StellaHuang95 reopened this Feb 27, 2023
@StellaHuang95
Copy link
Contributor

Hi @pschaeffer, the diagnostics for cyclical import chains is set to "none" by default to not slow down type analysis. You can go to Tools -> Options -> Python -> Analysis and change the Type checking mode to strict and reload the project to see the errors.

@StellaHuang95 StellaHuang95 added waiting for response and removed needs investigation Could be an issue - needs investigation labels Feb 28, 2023
@pschaeffer
Copy link

Hi,

I tried the suggested change. It made no difference. I did not know how to reload the project, so I shutdown VS 2022 completely and restarted it (first I switched to strict mode).

Thank you

Peter Schaeffer

@pschaeffer
Copy link

pschaeffer commented Feb 28, 2023 via email

@StellaHuang95
Copy link
Contributor

Great, glad that the issue has been resolved. And yes, reloading just means restart VS or close the project and open again.

@pschaeffer
Copy link

Hi,

I tried you suggested change. It did not resolve the problem. Indeed, it made no difference that I could tell.

Thank you

Peter Schaeffer

@StellaHuang95
Copy link
Contributor

StellaHuang95 commented Feb 28, 2023

@pschaeffer Could you share the screenshot? So you're saying that after you changed to strict mode, the circular import error didn't show in the error list? What's your diagnostic mode? Thanks!
image

@StellaHuang95 StellaHuang95 reopened this Feb 28, 2023
@pschaeffer
Copy link

pschaeffer commented Feb 28, 2023 via email

@pschaeffer
Copy link

Hi,

Let me offer another comment. I would think Python would report a circular import error in all cases (at runtime). However, this does not appear to be true.

@StellaHuang95
Copy link
Contributor

I wasn't able to repro this on my end, but I suspect that it may be related to the time it takes for Pylance to start the service instance and finish the diagnosis. In some cases, if the service instance isn't fully started, it may not be able to detect the cycle and the error won't appear . Could you be more specific on when you didn't see the ‘Cycle detected’ error message? Was this after loading the solution?

@pschaeffer
Copy link

I ran another test just a few minutes ago. Definitely still a problem. Should I send you the 'IDLM*' files so that you can reproduce the problem? My test environment is Visual Studio 2022 (PTVS). I also reproduced the same exact problem from the command line entirely outside of VS 2022. I can provide screen shots if that will help.

@StellaHuang95
Copy link
Contributor

Thanks, I would be happy to take a look at the files and screenshots you have, but based on what you've described, it sounds like the issue may not be related to PTVS or Visual Studio. Since you mentioned that you were able to reproduce the problem outside of VS as well, it may be outside of our scope to fix. Nonetheless, I'd be glad to take a look and see if I can offer any suggestions or guidance. Please feel free to send over the files and screenshots.

@pschaeffer
Copy link

pschaeffer commented Apr 20, 2023 via email

@StellaHuang95
Copy link
Contributor

I don't see the files. Could you try again and comment under this issue directly?

@pschaeffer
Copy link

Hello,

Here are the files. I put gif at the end of the py files because this system does not like py files. You will have to rename 3 files. All of the files to be renamed start with IDLM.

Thank you

Peter Schaeffer
Python Error Inside VS2022
Python Error Outside of VS2022
IDLmAJAX
IDLmBuild
IDLmUtility

@StellaHuang95
Copy link
Contributor

Could you try zip it up and send the .zip file?

@pschaeffer
Copy link

pschaeffer commented Apr 26, 2023 via email

@pschaeffer
Copy link

Hello Stella,

The actual file is a zipx file. However, I renamed it to be a jpeg file to get around upload restrictions.

Thank you

Peter Schaeffer
Python Problems

@StellaHuang95
Copy link
Contributor

I still couldn't access the files, were you able to upload .zip files? The supported file type seems include .zip. image

@pschaeffer
Copy link

Hello Stella,

Try again.

Thank you

Peter Schaeffer

Problems.zip

@StellaHuang95
Copy link
Contributor

Thanks, now I see the code. The issue is that you are trying to call the g() method directly from the module using IDLmUtility.g(), but g() is a static method in the IDLmUtility class. You can either change that to IDLmUtility.IDLmUtility.g() or change the import statement to from IDLmUtility import IDLmUtility to fix the error.

@pschaeffer
Copy link

pschaeffer commented Apr 27, 2023 via email

@pschaeffer
Copy link

Hello,

Thank you for the update. Shouldn’t I get some kind of import error with this structure?

Thank you

Peter Schaeffer

@StellaHuang95
Copy link
Contributor

Hi there, we wish we were able to thoroughly investigate all issues and leave all feature requests open indefinitely, but unfortunately, we have limited resources and it’s hard to organize and prioritize our work if we have a large number of issues open in our repository. If you find that this issue persists and falls within the scope of PTVS, could you please reopen it with the following details:

  1. Detailed reproduction steps with expected and actual behavior.

  2. Any sample code if different from the one provided.

  3. Go to Tools -> Python -> Analysis, set your Log Level to Trace, and share the logs.

Thank you for your understanding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants