Intellij doesn't resolve python imports from pip_install()
See original GitHub issuePython rules used from https://github.com/bazelbuild/rules_python
A pip dependency added to my target in deps. E.g.:
requirement("tensorflow")
import tensorflow
OR
from pypi__tensorflow as tensorflow
Both of them work with bazel
commandline; but Intellij IDE only resolves the second one with Bazel plugin.
Looking at the code, the behaviour makes sense.
Following patch reverses the IDE behavior (i.e. first import is resolved and second one isn’t- which seems correct behavior): In https://github.com/bazelbuild/intellij/blob/master/python/src/com/google/idea/blaze/python/resolve/provider/BazelPyImportResolverStrategy.java#L50:
diff --git a/python/src/com/google/idea/blaze/python/resolve/provider/BazelPyImportResolverStrategy.java b/python/src/com/google/idea/blaze/python/resolve/provider/BazelPyImportResolverStrategy.java
index ee0acbda..56d1f92c 100644
--- a/python/src/com/google/idea/blaze/python/resolve/provider/BazelPyImportResolverStrategy.java
+++ b/python/src/com/google/idea/blaze/python/resolve/provider/BazelPyImportResolverStrategy.java
@@ -22,6 +22,8 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.resolve.PyQualifiedNameResolveContext;
import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import javax.annotation.Nullable;
/**
@@ -32,6 +34,10 @@ import javax.annotation.Nullable;
*/
public class BazelPyImportResolverStrategy extends AbstractPyImportResolverStrategy {
+ // Packages installed with pip_install in python_rules are installed in a subdir that starts
+ // with this prefix.
+ private static final String PIP_INSTALL_DIR_PREFIX = "pypi__";
+
@Override
public boolean appliesToBuildSystem(BuildSystem buildSystem) {
return buildSystem == BuildSystem.Bazel;
@@ -51,6 +57,11 @@ public class BazelPyImportResolverStrategy extends AbstractPyImportResolverStrat
if (source.isGenerated() || !source.getRelativePath().endsWith(".py")) {
return null;
}
- return fromRelativePath(source.getRelativePath());
+ String relativePath = source.getRelativePath();
+ if (relativePath.startsWith(PIP_INSTALL_DIR_PREFIX)) {
+ Path path = Paths.get(relativePath);
+ relativePath = path.subpath(1, path.getNameCount()).toString();
+ }
+ return fromRelativePath(relativePath);
}
}
If it makes sense, I can send a pull request.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:13
- Comments:17 (1 by maintainers)
Top Results From Across the Web
problem to import modules – IDEs Support (IntelliJ Platform)
I have installed modules successfully via pip install numpy, for example. however, in the pycharm editor, "import numpy" does not work.
Read more >How do I get IntelliJ to recognize common Python modules?
Just create and add Python SDK File -> Project Structure -> Project -> Project SDK -> new. and select the installation path of...
Read more >Debugging PyCharm/Intellij IDEA no module named error ...
Pycharm is unable to recognize installed local modules, since python interpreter selected is wrong. It should be the one, where your pip ......
Read more >Databricks Connect | Databricks on AWS
The table shows the Python version installed with each Databricks Runtime. ... On Windows, if you see an error that Databricks Connect cannot...
Read more >How to Fix PyCharm Import Error and Setup Your Interpreter
This will show you how to fix common pycharm import errors when trying ... to install it with pip - You have multiple...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just leaving a comment for those looking for a workaround: I managed to workaround this issue by downgrading rules_python to
0.9.0
(this may not be a solution for everyone)I am using my the plugin compiled from my fork with this fix: https://github.com/sudopk/intellij
The fix works. I didn’t hear from bazel team, so didn’t take any further steps. If bazel team can include this fix, it will be easy. I can send a pull request, but adding tests will take me some more effort since I have never done plugin development.