VS Code / Pylance / Pylint Cannot resolve import
Solution 1
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 runninghelp(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.
Solution 2
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.
Solution 3
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.
xdhmoore
Was Java/JS, now TensorFlow w/ Python. PowerShell for fun. Love/loves: JavaScript. WSL, gVim. VS Code/Docker. Love/hates: PowerShell. SO at times, I'll admit. Also, PowerShell. Hablo español. Corrige mis errores, y yo aprendo. Me gustan haikus, pero sí fue difícil, hacerlos aquí.
Updated on June 09, 2022Comments
-
xdhmoore almost 2 years
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 ontf.__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 installingpylance
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?
-
Admin over 3 yearsGenerally issues like this can be solved with extensions, or sometimes it is the side effect of using the wrong one. Have you looked around for an extension that handles the kind of workflow you are trying to accomplish? It's entirely possible that the intellisense(s) you have installed are unable to understand the configuration you are working with.
-
xdhmoore over 3 yearsI'm using Microsoft's Python extension, the Remote - Containers extension, the Pylance extension, and others. I'm not sure what you mean by "issues like this can be solved with extensions". I believe these extensions that I have should solve the problem, but I'm having trouble configuring them correctly.
-
Admin over 3 yearsThere's an important caveat in what I said: you could have the wrong ones installed. Verify which extensions you have installed, check for any that may be incompatible. When in doubt, uninstall what you have, and selectively reinstall what you need. That's not something anyone can help you troubleshoot.