Fancy imports with tensorflow 1.15

Answered

Hello,

 

I use PyCharm for a while and recently I encountered some problems with inspector unable to see the modules imported from site-packages.

The case is pretty simple. I am using TensorFlow and after updating it to version 1.15.0, IDE's inspector can can no longer see the module named `compat`. The previous version of TensorFlow 1.14.0 worked perfectly.

The main difference is the whole module `tensorflow` has been split into two: 'tensorflow_estimator` (nothing special) and `tensorflow_core` (there the `compat` sub-module slumbers). The __init__ file of `tensorflow` package contains a wildcard import. As I know, there is nothing criminal and the IDE can track such imports.

 

However, when I try to import the `compat` submodule or something from it, the IDE shows this annoying message "No moduled named compat".

 

The virtual environment is correct (otherwise I wouldn't be able to import even tensorflow). When I excecute scripts they work. But as long as the inspector fails to find the sub-modules, I can only dream of automatic code completion and other handy functionality of the IDE.

 

Just for example, here how the TensorFLow 1.14.0 import looks. The `compat` is imported directly to the tensorflow __init__ file.

 

And of course with that old version everything works.

 

I really hope that there is an easy solution

 

Thank you in advance.

0
2 comments

Hi,

There is a known issue with skeletons generation for packages with underscores in their names https://youtrack.jetbrains.com/issue/PY-38454
It seems that it's the root cause of your issue as well. Please vote for it and follow for updates.

1
Avatar
Permanently deleted user

I found that this hack makes PyCharm happy with tensorflow 1.15.

from typing import TYPE_CHECKING
if TYPE_CHECKING:
import tensorflow_core.python as tf
else:
import tensorflow as tf

 

0

Please sign in to leave a comment.