'ImportError: cannot import name 'keras' from 'tensorflow'

I'm try learning TensorFlow but i have a problem. I'm importing TensorFlow like in official website but i take a error.

import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

Traceback (most recent call last):

File "C:\Users\Koray\Desktop\Python\main.py", line 4, in

from tensorflow import keras

ImportError: cannot import name 'keras' from 'tensorflow'

(C:\Users\Koray\Desktop\Python\tensorflow.py)



Solution 1:[1]

For community benefit, adding @John Gordon answer here in the answer section.

Don't name your script tensorflow.py. Your import finds that script instead of the real one.

Instead you can name any other for example learning.py. Now all your imports work as expected.

import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1