VSCode unable to autoimport python functions - python

I am currently able to use quick fix to auto import python functions from external typings such as from typing import List.
Python module quick fix import
However, I am unable to detect local functions/classes for import. For example: If I have the data class SampleDataClass in dataclasses.py, and I reference it in a function in test_file.py, VSCode is unable to detect it and I have to manually type out the import path for the dataclass.
Definition of dataclass
Reference to dataclass
I have the following extensions enabled:
Python
Pylance
Intellicode
My settings.json includes:
{
"python.envFile": "${workspaceFolder}/.env",
"python.languageServer": "Pylance",
"python.analysis.indexing": true,
"python.formatting.provider": "black",
"python.analysis.autoImportCompletions": true,
"python.analysis.autoSearchPaths": true,
"python.autoComplete.extraPaths": ["~/Development/<django repo name>/server"],
"python.analysis.extraPaths": ["~/Development/<django repo name>/server"],
"vsintellicode.features.python.deepLearning": "enabled",
}
I am using poetry for my virtual environment which is located at ~/Development/<django repo name>/.venv
Is there something that I'm missing?

Turns out the latest versions for Pylance broke quick-fix imports and any extra path settings for VSCode. When I rolled back the version to v2022.8.50 it now works again.
I filed an issue here: https://github.com/microsoft/pylance-release/issues/3353.

According to an issue I raised in github earlier, the developer gave a reply.
Custom code will not be added to the autocomplete list at this time (unless it has already been imported). This is done to prevent users from having too many custom modules, which may lead to too long loading time.
If necessary, you can start a discussion in github and vote for it.
Add:

Related

Problem in PyCharm with import function/class from standard module

I am coding in PyCharm and to set up the environment I use poetry, python version 3.10
Everything was working ok, but starting from one moment of time I got problem with module imports.
But the problem does not relate to the import of the whole module but to the import of class or function from module.
You can see as an example that BaseSettings from pydantic is underlined with red wavy line and Pycharm says that BaseSettings is unresolved reference.
I tried
to invalidate cache in Pycharm
install, uninstall the module (poetry add/remove)
change the environment and return back
marked folder as a source root (assume it is irrelevant but anyway)
but unfortunately nothing helped
aiohttp.client.py
aiohttp.init.py
I found the out what was the reason for this strange behaviour. It was the problem of PyCharm IDE. PyCharm stopped recognising init.py as python files that produced import errors. Actually I do not know why this happened since I did not changed any settings or updated PyCharm.
The solution was to manually add pattern "init.py" to python files mask in Settings -> Editor ->File types -> Python (please see screenshot in the attachment)

Using VisualStudio+ Python -- how to handle "overriding stdlib module" Pylance(reportShadowedImports) warning?

When running ipynbs in VS Code, I've started noticing Pylance warnings on standard library imports. I am using a conda virtual environment, and I believe the warning is related to that. An example using the glob library reads:
"env\Lib\glob.py" is overriding the stdlib "glob" modulePylance(reportShadowedImports)
So far my notebooks run as expected, but I am curious if this warning is indicative of poor layout or is just stating the obvious more of an "FYI you are not using the base install of python".
I have turned off linting and the problem stills persists. And almost nothing returns from my searches of the error "reportShadowedImports".
The reason you find nothing by searching is because this check has just been implemented recently (see Github). I ran into the same problem as you because code.py from Micropython/Circuitpython also overrides the module "code" in stdlib.
The solution is simple, though you then loose out on this specific check. Just add reportShadowedImports to your pyright config. For VS Code, that would be adding it to .vscode/settings.json:
{
"python.languageServer": "Pylance",
[...]
"python.analysis.diagnosticSeverityOverrides": {
"reportShadowedImports": "none"
},
[...]
}

pylance: import could not be resolved

I am using vscode with virtual environment (conda) to write python files on my mac.
I keep seeing import problems from pylance for specific third party AND official libraries (for example pyproj, geopy for third party, math for preinstalled).
for each file the errors/warnings look as follows:
I am sure that these packages are in my virtual environment (they are included in the conda list packages.
Although so far it seems to me as a cosmetic issue, removing these warnings would be very helpful.
Here (https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#unresolved-import-warnings) there are some explanations but I did not fully udnerstand the issue or managed to resolve.
Add the following configuration in setting.json to remove these warnings
"python.analysis.diagnosticSeverityOverrides": {
"reportMissingImports": "none",
"reportUndefinedVariable": "none"
},
Use ctrl + shift +p to open the command palette. Search Settings, there are two settings.json files here. User works globally, Workspace works only on the current workspace. If the same configuration exists in both files, Workspace will override the settings in User.

VS Code / Pylance / Pylint Cannot resolve import

The Summary
I have a python import that works when run from the VS Code terminal, but that VS Code's editor is giving warnings about. Also, "Go to Definition" doesn't work.
The Problem
I have created a docker container from the image tensorflow/tensorflow:1.15.2-py3, then attach to it using VS Code's "Remote- Containers" extension. Then I've created the following file in the container.
main.py:
import tensorflow.compat.v1 as tf
print(tf.__version__)
This runs fine in the VS Code terminal, but the Editor and the Problems pane both give me an unresolved import 'tensorflow.compat' warning. Also "Go to Definition" doesn't work on tf.__version__.
I'm using several extensions but I believe the relevant ones are the Microsoft Python extension (installed in the container), as well as the Remote - Containers extension, and now the Pylance extension (installed in the container).
The Things I've Tried
I've tried this with the default pylint, and then also after installing pylance with similar results. I've also seen some docs about similar issues, but they were related to setting the correct source folder location for modules that were part of a project. In contrast, my code within my project seems to work fine with imports/go-to-definition. It's external libraries that don't seem to work.
Also, for the sake of this minimal example, I've attached to the container as root, so I am guessing it's not an issue of elevated permissions.
I've also tried disabling all the extensions except the following, but got the same results:
Remote - Containers (local)
Remote - WSL (local)
Python (on container)
Jupyter (on container, required by Python for some reason)
All the extensions above are on the latest versions.
I've also fiddled around with setting python.autocomplete.extraPaths, but I'm not sure what the right path is. It also seems like the wrong thing to have to add libraries to the path that are installed in the global python installation, especially since I'm not using a virtual environment (it being in a docker container and all).
The Question
How do I fix VS Code so that it recognizes this import and I can use "Go to Definition" to explore these tensorflow functions/classes/etc?
tldr;
TensorFlow defines some of its modules in a way that pylint & pylance aren't able to recognize. These errors don't necessarily indicate an incorrect setup.
To Fix:
pylint: The pylint warnings are safely ignored.
Intellisense: The best way I know of at the moment to fix Intellisense is to replace the imports with the modules they are aliasing (found by importing alias in a repl as x then running help(x)). Because the target of the alias in my case is an internal name, you probably don't want to check in these changes to source control. Not ideal.
Details
Regarding the linting: It seems that tensorflow defines its modules in a way that the tools can't understand. Also, it appears that the package is an alias of some kind to another package. For example:
import tensorflow.compat.v1 as tf
tf.estimator.RunConfig()
The above code gives the pylint warning and breaks intellisense. But if you manually import the above in a REPL and run help(tf), it shows you the below package, which you can use instead:
import tensorflow_core._api.v1.compat.v1 as tf
tf.estimator.RunConfig()
This second example does not cause the pylint warning. Also the Intellisense features (Go to definition, Ctrl+Click, etc) work with this second example.
However, based on the _api, it looks like that second package name is an internal namespace, so I'm guessing it is probably best to only use this internal name for local debugging.
Confirmation/Tickets
pylint: I've found a ticket about pylint having issues with a couple tensorflow imports that looks related.
Intellisense: I've opened a ticket with pylance.
So for me I was trying to
import pandas as pd
but I got the error
"pd" is not accessedPylance (module) pd
SO what I did was reload the extension Python IntelliSense(Pylance) and that solved my issue.
I had the same problem but with all kinds of packages.
My solution was to go to the VSCode settings and search for "python.analysis.extraPaths", and add the path to your site-packages.
In my case, I added C:\Code\Python39\Lib\site-packages, and now it's working fine.
What, usually, solves the pylance issues for me is pointing my Python interpreter to the virtualenv one.
Open the command palette Ctrl + Shift + P
Type: Python: Select Interpreter
It will show a list of all the python Interpreters it actually detects:
Select Enter interpreter path
Type in the path to your local venv/bin folder or click find to navigate using the file explorer.
Your path should look something like:
venv/bin/python3.9
i changed "import tensorflow as tf" to "from tensorflow import compat as tf"
it ll even work for tf.gfile.Gfile()

SublimeLinter pep8_ignore not working

I want SublimeLinter to ignore PEP-8 E402 error messages. I have changed the package user settings to
"pep8_ignore": [
"E402"
]
and have closed and reopened Sublime Text. SublimeLinter still displays an E402 error code. Why?
I am using ST3 with these packages: BufferScrool, LaTeXing, PackageControl, PackageResourceViewer, R-Box, Side Bar, Anaconda, and SublimeLinter.
So first, I'd note that Anaconda performs its own version of PEP-8 linting, so you've got some functional duplication happening...
Second, I'd note that you don't seem to have SublimeLinter-pep8 installed, which means (since SublimeLinter installed by Package Control on ST3 is going to be SublimeLinter3, which requires linting plugins to do anything) that its not SublimeLinter. Things to consider: (1) are the linting errors you see coming from Anaconda rather than SublimeLinter? If so you need to set "pep8_ignore" in the Anaconda user settings rather than SublimeLinter. (2) Are you (somehow) using the old, unsupported version of SublimeLinter on ST3? In that case, either (a) switch to using Anaconda and ignore SublimeLinter for the purposes of linting python files or (b) upgrade SublimeLinter to SublimeLinter3, install SublimeLinter-pep8 and see below.
Assuming you are actually trying to use the SublimeLinter-pep8 plugin on SublimeLinter3: SublimeLinter3, which uses an entirely different settings structure from the previous version of SublimeLinter. As a result, the "pep8_ignore" setting is no longer honoured. This is the expected behaviour.
In order to accomplish what you want you need these settings set in the settings file:
{
"user": {
"linters": {
"pep8": {
"ignore": "E402"
}
}
}
}
But have a careful read of the SublimeLinter settings docs as there are a lot of different ways to configure your settings, and have a look at the docs for the pep-8 plugin
As ig0774 mentioned, Anaconda has its own built-in linters (pylint, pep8, pep257, and pyflakes), which are producing the error message you'd like to ignore, not SublimeLinter. If for some reason you want to shut off linting with Anaconda and perform it with one or more SublimeLinter plugins, you'll need to install and configure those plugins. Search Package Control for more information.
To configure Anaconda, first open Preferences -> Package Settings -> Anaconda -> Settings-Default, then Settings-User in the same sub-menu. Copy the entire contents of Default to User, then close Default, as you can't edit it anyway. I highly recommend reading through the entire file, as it is well-commented. The setting you are looking for is also called "pep8_ignore", which in the current version of Anaconda is on line 318. This file seems to change rather often, so this position may change over time. Just search for the name.
If you want to turn off Anaconda's linting capabilities completely, set "anaconda_linting" to false.

Categories

Resources