0

How to install tensor-keras using conda? I have below error message. I see this error while importing below module Here i copied a snipped of my code what i am actually trying in my code

from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow as tf

callback=tf.keras.callbacks.ModelCheckpoint(filepath='Regressor_model.h5',
                                                monitor='mean_absolute_error',
                                                verbose=0,
                                                save_best_only=True,
                                                save_weights_only=False,
                                                mode='auto')

I was successfully installed tensorflow in Anaconda prompt by using below commands.

conda create -n tf tensorflow
conda activate tf
conda install keras

By the way i restarted pycharm IDE a couple a times. I still have same error

conda create -n tf tensorflow conda activate tf conda install keras

Traceback (most recent call last):
  File "C:\Users\rethi1\Anaconda-python\lib\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-353837d93f8d>", line 1, in <module>
    runfile('C:/Users/rethi1/PycharmProjects/pythonProject/stock_prediction_test/__init__.py', wdir='C:/Users/rethi1/PycharmProjects/pythonProject/stock_prediction_test')
  File "C:\Program Files\JetBrains\PyCharm 2020.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2020.2.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/rethi1/PycharmProjects/pythonProject/stock_prediction_test/__init__.py", line 273, in <module>
    from tensorflow.keras.callbacks import ModelCheckpoint
  File "C:\Program Files\JetBrains\PyCharm 2020.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'tensorflow.keras'

I tried a simple code which imports and uses tensorflow module

import tensorflow as tf

# Initialize two constants
x1 = tf.constant([9,10,11,12])
x2 = tf.constant([5,6,7,8])

# Multiply
result = tf.multiply(x1, x2)

# Print the result
print(result)

Error looks similar to above unable to use the module

x1 = tf.constant([9,10,11,12])
AttributeError: module 'tensorflow' has no attribute 'constant'

Thanks in Advance!

Revanth Tv
  • 63
  • 8
  • Can you actually import tensorflow? Because if that does not work, no module under tensorflow will work anyway – Dr. Snoopy Nov 04 '20 at 18:17
  • Good point. I updated a piece a code above again which i have tried after reading your comment. I am using Python 3.7.9 I knew tensorflow is supported upto python 3.8.0 Please correct me if i am wrong. Thanks! – Revanth Tv Nov 04 '20 at 21:34
  • Make sure you do not have a script named tensorflow.py inside your own custom code/scripts. – Dr. Snoopy Nov 04 '20 at 21:56
  • I have it many places: For example see below \Anaconda-python\Lib\site-packages\opt_einsum\backends\tensorflow.py \Anaconda-python\envs\tf\Lib\site-packages\opt_einsum\backends Should i delete this and try executing again? – Revanth Tv Nov 05 '20 at 06:18
  • No, I said inside your own custom code/scripts, not in installed packages. – Dr. Snoopy Nov 05 '20 at 10:42
  • No, I do not have tensorflow.py at any other location except in my installed packages – Revanth Tv Nov 05 '20 at 12:39
  • then do this after importing tensorflow, print(tensorflow), and report back the results – Dr. Snoopy Nov 05 '20 at 13:21
  • Thankyou very much for replying back everytime. I figured out a soultion which is working now. I downgraded my py version to python 3.6.8 Recommended to follow: https://www.tensorflow.org/install/pip#system-install Reasone for the issue is very obvious. Most of the users might experience the same problem. Usage of non compatable version which are completely out of bound. – Revanth Tv Nov 08 '20 at 09:51

0 Answers0