Score:-1

Pycharm false import error alarm although code executes

vn flag

I am new to PyCharm IDE (accustomed to VS code). I am getting an import error alarm on IDE with the following line of code:

from tensorflow.keras.preprocessing.text import Tokenizer
Error: Cannot find reference 'keras' in '__init__.py'

But when I actually hit run, it perfectly prints the output without any error on IDE.

I am in a virtual env "/home/instantinopaul/.virtualenvs/tfcert/bin/python" (same in project interpreter settings) and it points to /usr/bin/python3.8

Tensorflow is installed in the virtual environment only and not globally.

Version details: Python version: 3.8 Tensorflow version: 2.9.0 PyCharm 2023.1 (Community Edition) Build #PC-231.8109.197, built on March 29, 2023 Runtime version: 17.0.6+10-b829.5 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Linux 5.19.0-38-generic GC: G1 Young Generation, G1 Old Generation Memory: 1988M Cores: 12 Registry: debugger.new.tool.window.layout=true ide.experimental.ui=true

Current Desktop: ubuntu:GNOME Ubuntu 22.04

David avatar
cn flag
What is the version of Ubuntu?
Sayan Dey avatar
vn flag
It is ubuntu 22.04
Sayan Dey avatar
vn flag
This is a known [issue](https://github.com/tensorflow/tensorflow/issues/53144) with tensorflow. Answered [here](https://stackoverflow.com/a/71838765/13717851)
David avatar
cn flag
Sounds like you should use the Answer Your Own Question feature at the bottom of your question and make this an answer.
Score:0
vn flag

This is a known issue of auto-completion error for all IDEs (at-least for Vs code and pycharm) since tf 2.6 it seems. This happens because tensorflow loads keras via lazyloading fashion, which the IDEs cannot resolve.

Please check this issue thread on github PyCharm doesn't resolve anything under tensorflow.keras

There are two workarounds with this issue

  1. Try modify the file site-packages/tensorflow/init.py near line 387 mentioned on above thread.
  2. Using import keras.api._v2.keras as keras since this seems to be the exact package tensorflow loads itself.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.