ModuleNotFoundError: No module named 'tensorflow.compat'

anyone helping me resolve this issue?

I am trying to get my code running on virutal env. on Pycharm, but continue to run into ModuleNotFoundError: No module named ‘tensorflow.compat’.

the snippet is below.

import numpy as np
import tensorflow as tf
from keras.models import Sequential
from sklearn.model_selection import train_test_split

class _lstm_model:
def init(self):
self._x = None
self._y = None
def Run_Model(self):
n_feature = 1
X_train, X_test, Y_train, Y_test = train_test_split(self._x, self._y, test_size=0.3, random_state=10)
model = Sequential()


and this is callstack I got
-------------------------------------------------------------------------------.
Traceback (most recent call last):
File “C:\Users\c_kyu\Development\PersonalProject\ML\stockmarket\Runner\Runner.py”, line 5, in
from Models.lstm_model import lstm_model
File “C:\Users\c_kyu\Development\PersonalProject\ML\stockmarket\Models_lstm_model.py”, line 5, in
from keras.models import Sequential
File "C:\Users\c_kyu\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras_init
.py", line 21, in
from keras import models
File "C:\Users\c_kyu\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\models_init
.py", line 18, in
from keras.engine.functional import Functional
File “C:\Users\c_kyu\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\engine\functional.py”, line 24, in
import tensorflow.compat.v2 as tf
ModuleNotFoundError: No module named ‘tensorflow.compat’

I am googling for a bit to find the solution, but was failing with all solutions being said online I found.

I am running into an error ModuleNotFoundError: No module named ‘tensorflow.compat’ on pycharm.
my code is pretty simple, the below is snippet.

from keras.models import Sequential

from keras.layers import *

import numpy as np
import tensorflow as tf
from keras.models import Sequential
from sklearn.model_selection import train_test_split

class _lstm_model:

@Ted,

Welcome to the Tensorflow Forum!

You can configure or select the installed python path through python interpreter in Pycharm by follwing the configure python interpreter.

To use Keras, you will need to have the Tensorflow package installed. Once Tensorflow is installed, just import Keras as shown below

from tensorflow import keras

Thank you!

1 Like

emmmm……I’ve already installed tensorflow2.8, but the error also appears.
And this is my code:
import tensorflow as tf
import tensorflow.python.keras as keras
from keras.models import Sequential, Model

The first and second can run, but the third not, and No module named ‘tensorflow.compat’ appears.

So have you solved this problem? :thinking:

@729306137,

Welcome to The Tensorflow Forum!

Please try as shown below and let us know?

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential, Model

Thank you!

Yeah I had the issue resolved.
For my case, first time I ran into the issue, thought the same. even though I have all packaged installed well, I was still having the issue.
Btw, with Tensorflow team help, I ended up finding out that a configuration on the Pycharm I am using was wrong. I switched to the right virtual env from the bottom of it, but it was not working well for some reasons, since then, I switched to the right virtual env where had the packaged installed from the Interpreter setting again, I got the issue sorted out

OKay, thanks for your answer. I’m using Anaconda right now, and this may also be some kind of configuration problem. I’m going to try the method you suggest to solve it. Thanks again! :grinning:

1 Like

Btw, I’ve got the problem solved right now. The original file name was ‘hw3.ipynb’, and I changed it into ‘homework3.ipynb’. That just works! Really strange.

1 Like

I tried this but its not working for me i am using python 3.9 with tensorflow 2.14 version i am getting the following error ,
import tensorflow.compat.v2 as tf
ModuleNotFoundError: No module named ‘tensorflow.compat’

please help

Hi @729306137, I was able to execute the above import statement without any error in colab which uses Linux environment. Could you please provide the details about the OS , environment details and steps followed to install tensorflow. Thank You.

I am using pycharm in windows 11,i have installed package for tensorflow (2.14) in pycharm ,I was trying to use model.save() but it was throwing me error cannot import version from tensorflow.python.keras ,after fixing that i am getting this error

Bro iam aslo facing the same problem
“ModuleNotFoundError: No module named ‘tensorflow.compat’”
i have used all the methods to resolve the error but no use i cant load my model
Please help

I believe that could be a development circumstance issue, so you should make sure if you selected the right virutal environment on your IDE where the packages well are installed, the way how to figure out is depends on the IDE you are using.