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

'module' is not Accessed Pylance(Pylance is greyed out) #757

Closed
pybash1 opened this issue Dec 21, 2020 · 51 comments
Closed

'module' is not Accessed Pylance(Pylance is greyed out) #757

pybash1 opened this issue Dec 21, 2020 · 51 comments
Labels
needs investigation Could be an issue - needs investigation

Comments

@pybash1
Copy link

pybash1 commented Dec 21, 2020

When I am importing the python module 'requests'. PyLance or VSCode underlines it with yellow meaning warning. It is also shown in the problems panel in the integrated terminal of vscode. Hope it gets fixed soon. I did not use the Bug Report Template because I am not sure if it is a bug.
Refference Image or Screenshot

@pybash1 pybash1 changed the title 'requests' is not Accessed Pylance(Pylance is greyed out) 'module' is not Accessed Pylance(Pylance is greyed out) Dec 21, 2020
@pybash1
Copy link
Author

pybash1 commented Dec 21, 2020

Edit 1: While importing the module 'wget', the exact same thing happens. So does this mean that it happens with such type of web modules?

@erictraut
Copy link
Contributor

This isn't a bug. The warning is telling you that the source code for this module can't be resolved within your configured Python environment. It is likely that either your configuration is wrong or the module isn't installed in that environment. If you don't care about this warning, you can disable it in the settings or configuration file.

@erictraut
Copy link
Contributor

And the reason it's grayed out is because no other code in your source file is referring to requests.

@pybash1
Copy link
Author

pybash1 commented Dec 21, 2020

But how can you say that it is not installed in that environment when running the application and even using the module dosent give any error. And I meant that the word Pylance is greyed out. Not the module I know I didn't reffer to it. But I later I did and the code worked and ran fine. So could tell about that?

@erictraut erictraut transferred this issue from microsoft/pyright Dec 21, 2020
@pybash1
Copy link
Author

pybash1 commented Dec 22, 2020

I guess I should not ask this... But why did you transfer this issue?

@judej
Copy link
Contributor

judej commented Dec 22, 2020

Pylance is greyed out because Pylance is the reporter of the error. With regard to the unresolved import warning, please take a look at the trouble shooting guide: https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#unresolved-import-warnings

@judej judej added the waiting for user response Requires more information from user label Dec 22, 2020
@github-actions github-actions bot removed the triage label Dec 22, 2020
@pybash1
Copy link
Author

pybash1 commented Dec 23, 2020

Ok..that explains it. Thanks @judej

@pybash1
Copy link
Author

pybash1 commented Dec 23, 2020

By the way could you tell me what more info you need? I would be happy to help you... Because you added the waiting for info label...

@jakebailey
Copy link
Member

Our issue template typically asks for logs; you may have selected the "question" which is more discussion based. I'd be interested in seeing some trace logs to see what paths are being used and what files are analyzed.

https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#filing-an-issue mirrors the template instructions.

@pybash1
Copy link
Author

pybash1 commented Jan 17, 2021

Update -
After changing my interpreter from Python 3.7 to Anaconda Python 3.8.3 in VSCode

The issue was that I had python 3.7 selected in VSCode as my default interpreter but my os's default interpreter was conda 3.8.3 so all of my packages were installed there. But since Py 3.7 was selected in VSCode, Pylance thought it was not installed since it was installed for conda 3.8.3 and were not for py 3.7.

But due to some odd reason running it with 3.7 didn't give any error and was able to use the modules.

But I dont know why Pylance does this when I am importing a local file.

@pybash1
Copy link
Author

pybash1 commented Jan 17, 2021

@erictraut was actually correct in his 2nd comment

@jakebailey
Copy link
Member

Your original issue was about requests, which isn't a local module (it'd be an installed package). You're now mentioning a local import, can you describe this setup at all? Are you assuming some sys.path modification that needs to be configured so we can find the import? https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#unresolved-import-warnings

@pybash1
Copy link
Author

pybash1 commented Jan 29, 2021

Ok, let me explain.


Terms used:
pip - pip3
Py, py - Python3
Conda, conda - Anaconda3


First I was trying to import requests, wget and some more modules. But it showed this error. Now few days back I just found out that inside VSCode my python interpreter was selected as Py3.7. But conda3.8.3 was my windows default interpreter. And thus all modules installed using pip were installed there. But PyLance tried to find in Py3.7 and they were not present. But the scripts never showed an error because when I was executing them through the terminal, they actually getting executed in Conda3.8.3. So when i changed my interpreter from py3.7 to conda3.8.3 in vscode all those errors were gone. And that fixed it.


But now if I try to import a local file, pylance shows the error. Executing it doesn't give any error. And workd fine. But Pylance Shows the error.


Hope this helps you. So could you fix so that importing local modules doesn't give an error. And also, PyLance says, this is a warning not an error. If you want I can attach a screenshot. Not using an virtual envirionment


A suggestion - could you make the warning underlines yellow instead of green, that would be better. Since, green kind of means there are no issue.

@pybash1
Copy link
Author

pybash1 commented Jan 29, 2021

Don't know why everything became a "h1"?

@jakebailey
Copy link
Member

Pylance respects the interpreter chosen by the Python extension; if you have the wrong interpreter selected then we can't really do anything about that.

We want to show import resolution messages as they significantly impact the analysis. If you really don't care about import warnings, you can use diagnosticSeverityOverrides and silence them. This to me is a bad idea, as it means all of the info you're working with is silently wrong.

For local modules, your project layout may mean you need to set extraPaths: https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#common-questions-and-issues

RE: h1, github issues are markdown, and putting ---- on the line after text makes it a header. I've edited your comment to make it readable again.

@pybash1
Copy link
Author

pybash1 commented Jan 31, 2021

Thanks for the edit... yeah i know the interpreter was my bad, sorry. But I don't know about the local modules. I can set extraPaths, but previously I never had to that. So I said. And the warnings are yellow, they are green because of my theme. And I used diagnosticSeverityOverrirdes and set it to information. that way it doesn't show a warning and I still get the info. Thanks. But could you make it so that I don't need to set extraPath for local modules.

@jakebailey
Copy link
Member

The thing I'm curious about is "previously I never had to that"; previously meaning what? With a previous version of Pylance? With another language server setting? In another editor?

@pybash1
Copy link
Author

pybash1 commented Feb 2, 2021

The thing I'm curious about is "previously I never had to that"; previously meaning what? With a previous version of Pylance? With another language server setting? In another editor?

"previously I never had to do that" means with another language server. That is with the default Python extension from Microsoft.

@jakebailey
Copy link
Member

I see. The previous default is Jedi, which has a pretty loose/forgiving import resolution system that favors resolving modules rather than erroring when they may not actually work at runtime. For local stuff, Pylance's behavior matches MPLS and other analyses that are more strict and try to inform of these issues. I don't know that we're likely to try and change that behavior, as we try to be as spec-compliant as possible (even if some projects require some extra configuration; most do not). The troubleshooting docs do describe this "import not resolved" message as much as possible for this reason.

What'd be most helpful is a description of your repo layout, in case it's something we can attempt to detect or handle through other means.

@pybash1
Copy link
Author

pybash1 commented Feb 6, 2021

What do you mean by repo layout? the file structure or hierarchy?

I don't know what the default one is? But I installed it from extension marketplace, named Python and author Microsoft.

@jakebailey
Copy link
Member

The folder layout, and how the files import each other.

I'm talking the languageServer setting; Pylance is an option you had to configure to enable. If unset, that means jedi.

@pybash1
Copy link
Author

pybash1 commented Feb 9, 2021

Re: I didn't configure Pylance manually I just installed it from the extension store.

@pybash1
Copy link
Author

pybash1 commented Feb 9, 2021

The Folder Layout:
A lot to type, so attaching images.

Folder Layout:
image

Files and their Imports(the programs_start directory is not included because they do not import anything and are not part of the project):
main.py:
image
files.py:
image
windows.py:
image
web_search.py:
image
query_finder.py:
image
programs.py:
image

@limdauto

This comment has been minimized.

@erictraut

This comment has been minimized.

@limdauto

This comment has been minimized.

@pybash1
Copy link
Author

pybash1 commented Mar 3, 2021

any updates??

@jakebailey
Copy link
Member

We are currently exploring ways in which we can improve our import resolution, or at least provide more guidance to fixing it. In your case, I did suggest extraPaths to mark extra import roots (which is our current suggestion) and we don't have any other guidance for when a project like this imports with unclear semantics (see also #253).

If there are updates, we'll put them here, but I'm fairly certain what you're seeing is a part of our current design and requires you to set extraPaths as is hinted in our troubleshooting docs.

@jakebailey jakebailey added needs investigation Could be an issue - needs investigation and removed waiting for user response Requires more information from user labels Mar 3, 2021
@pybash1
Copy link
Author

pybash1 commented Mar 4, 2021

ok... and will this issue be closed, since it is kind of resolved??

@jakebailey
Copy link
Member

If you mean that it's resolved now with extraPaths, then sure, we can close this issue.

@pybash1
Copy link
Author

pybash1 commented Mar 10, 2021

ok then, close it... thanks

@pybash1
Copy link
Author

pybash1 commented Apr 16, 2021

I just saw this today

from . import module doesn't give the error!

@jakebailey
Copy link
Member

Yes, relative imports don't need paths to be able to resolve, as they know where they're relative to (the current module's folder). Using relative imports bypasses the problem.

I'm not sure why I didn't close this issue after you asked for it, oops. #68 and #253 largely supersede this issue, in any case.

@pybash1
Copy link
Author

pybash1 commented Apr 17, 2021

Alright, thanks!

@Macanthony7
Copy link

please i can't seem to solve this part. >

State the environment where your code is running; i.e. Python version, the virtual environment type, etc.
If using a virtual environment, please include the requirements.txt file.
If working with a conda environment, attach the environment.yml file.
A code example (or any other additional information) we can use to reproduce the issue.

@pybash1
Copy link
Author

pybash1 commented Jul 6, 2021

please i can't seem to solve this part. >

State the environment where your code is running; i.e. Python version, the virtual environment type, etc.
If using a virtual environment, please include the requirements.txt file.
If working with a conda environment, attach the environment.yml file.
A code example (or any other additional information) we can use to reproduce the issue.

I can't understnad what you cant resolve?

@Macanthony7
Copy link

Macanthony7 commented Jul 8, 2021 via email

@pybash1
Copy link
Author

pybash1 commented Jul 10, 2021

@jakebailey Could you please help @Macanthony7 ?

@jakebailey
Copy link
Member

@Macanthony7 I don't think your issue is related to this one; can you please file a new issue and fill out the bug report template?

(The template you appear to be quoting isn't ours.)

@Ksp-3086
Copy link

Ksp-3086 commented Sep 7, 2021

python
I have been facing the same import issue with any library module.

  1. There is only 1 version of python installed 3.8.10 and pylance is also installed.
  2. It was working before, as I am new to python and I have been practicing different codes, so before import worked fine but now even import numpy as np, OR from numpy import linspace, pi, ... don't work.
    Anyone got a fix for this because none of the above discussion helped out

@jakebailey
Copy link
Member

"Not accessed" is a visual indicator that says you haven't used the import, not that it didn't resolve or that your code won't run. The "fix" is either to remove the import or to use it, but this is only a visual indicator.

@Ksp-3086
Copy link

Ksp-3086 commented Sep 8, 2021

asarray error
ok, but if I use asfarray which is imported from numpy, it says not defined.

@jakebailey
Copy link
Member

You need to write numpy.asfarray; that code will not execute.

@Ksp-3086
Copy link

Ksp-3086 commented Sep 8, 2021

ok, thanks. It worked

@hyphaestion
Copy link

Hey, I had this same problem. For others that are more visual learners, I followed the steps in this video https://www.youtube.com/watch?v=GqTsFOtZiQI

I was able to resolve the issue. Thank you for posting this thread, because you were able to help me identify the problem which was the interpreter that was being used.

Ok, let me explain.

Terms used: pip - pip3 Py, py - Python3 Conda, conda - Anaconda3

First I was trying to import requests, wget and some more modules. But it showed this error. Now few days back I just found out that inside VSCode my python interpreter was selected as Py3.7. But conda3.8.3 was my windows default interpreter. And thus all modules installed using pip were installed there. But PyLance tried to find in Py3.7 and they were not present. But the scripts never showed an error because when I was executing them through the terminal, they actually getting executed in Conda3.8.3. So when i changed my interpreter from py3.7 to conda3.8.3 in vscode all those errors were gone. And that fixed it.

But now if I try to import a local file, pylance shows the error. Executing it doesn't give any error. And workd fine. But Pylance Shows the error.

Hope this helps you. So could you fix so that importing local modules doesn't give an error. And also, PyLance says, this is a warning not an error. If you want I can attach a screenshot. Not using an virtual envirionment

A suggestion - could you make the warning underlines yellow instead of green, that would be better. Since, green kind of means there are no issue.

@richardjharris
Copy link

richardjharris commented Jun 1, 2022

I removed a line in __init__.py marked with "Import ... is not accessed" and it broke both my unit tests and my command line scripts.

So I have yomikun/commands/parse_custom_data.py that does import yomikun.custom_data and then calls yomikun.custom_data.parse_file(...)

And yomikun/custom_data/__init__.py contains from .csv import parse_file - this is the line pylance complains about

yomikun/custom_data/csv.py defines the parse_file method

Is there something I am missing? I have not configured extraPaths, but the code seems clearly referenced to me.

@erictraut
Copy link
Contributor

The import is grayed out as a subtle way of indicating that there is no local reference to that imported symbol within the file. This is not flagged as an error (i.e. there's no red squiggle beneath it) because there are legitimate reasons why one might not reference an import locally — for example, if the intent is for the imported symbol to be re-exported to other modules. So don't think of a grayed-out statement as a "bug that needs to be fixed". Think of it instead just as a subtle hint that there is no local reference to that symbol. Sometimes that's intended and sometimes it's not. If it is intended, then ignore it. If it's not intended (e.g. it's an outdated import statement that you forgot to remove), then delete it.

@Aswini327
Copy link

Aswini327 commented Sep 13, 2023

icon_id not accessed pylance
API_key not accessed pylance
Programme writes in VS Code
What happens in above lines please tell me

@debonte
Copy link
Contributor

debonte commented Sep 13, 2023

@Aswini327, please file a new issue and include a code sample that reproduces the problem that you are seeing.

@abhinay181
Copy link

abhinay181 commented Oct 19, 2023

@jakebailey @pybash1 @erictraut @debonte

I am unable to access methods from sqlalchemy package. I have installed the package successfully you can see it in the below screenshot I have run the pip list command to show that sqlalchemy is installed

new 2
new 3
new 4

@debonte
Copy link
Contributor

debonte commented Oct 19, 2023

@abhinay181, since this is a runtime issue, it would be better to ask the library maintainers over at the sqlalchemy repo -- https://github.com/sqlalchemy/sqlalchemy/discussions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation Could be an issue - needs investigation
Projects
None yet
Development

No branches or pull requests