When I try to import tensorflow in my Python scripts, I have some weird results. For instance:
import tensorflow
from keras.datasets import imdb
gives me
ModuleNotFoundError
Traceback (most recent call last) <ipython-input-12-25cf0f878919> in <module>()
1 import tensorflow
----> 2 from keras.datasets import imdb
ModuleNotFoundError: No module named 'keras'
If I try:
import tensorflow as tf
from tf.keras.datasets import imdb
I get :
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-9-bd3db3d3567b> in <module>()
1 import tensorflow as tf
----> 2 from tf.keras.datasets import imdb
ModuleNotFoundError: No module named 'tf'
But, if I use :
from tensorflow.keras.datasets import imdb
it works.
I've been googling this for a full hour now, and I still don't understand what I'm doing wrong in the first two scripts.
Thanks
You haven't specified how and where you installed tensorflow, so I could be wrong, but:
(a) it appears that keras is installed with tensorflow, but not in a location that is in the default Python path (hence, you cannot do from keras.datasets import imdb).
(b) this combination:
import tensorflow as tf
from tf.keras.datasets import imdb
is invalid, because from x import y searches for x as a module and not as a symbol in your code's globals (and tf is NOT a module name but a global variable, import tensorflow as tf imports tensorflow and sets tf to point to the module object).
Therefore (unless you fix your install or your PYTHONPATH to make keras visible as a module), you should use this to import keras (or specific symbols from it):
# either this, to access keras.*, e.g., keras.datasets.imdb
import tensorflow.keras as keras
# or this, as you've done in your example
from tensorflow.keras.datasets import imdb
Related
I'm currently building a MLP network using the MNIST data set in python. My script can import the mnist module and access the datasets when it's running in the default runtime environment but whenever I try to run it using pypy I get the error
Traceback (most recent call last):
File "LearningProgram.py", line 1, in <module>
from mnist import MNIST
ModuleNotFoundError: No module named 'mnist'
This is how I import mnist if it matters
from mnist import MNIST
import random
import numpy as np
import math
from math import e
from math import log
I've tried uninstalling and reinstalling mnist but it didn't work. I'm also using python 3.8.1
I am following this tensorflow tutorial https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html#protobuf-installation-compilation and I am facing some problems when testing the installation.
I installed everything except the COCO API in the tutorial and I am running the object detection demo in jupyter right now. For some reason, I get an error inside the notebook which tells me that there is "No module named 'tensorflow'". Can I still click on Run All or does this error has to be fixed?
Thanks! :)
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import ops as utils_ops
if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):
raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-34f5cdda911a> in <module>
4 import sys
5 import tarfile
----> 6 import tensorflow as tf
7 import zipfile
8
ModuleNotFoundError: No module named 'tensorflow'
Okay, for some reasons, I get this error now:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-34f5cdda911a> in <module>
10 from collections import defaultdict
11 from io import StringIO
---> 12 from matplotlib import pyplot as plt
13 from PIL import Image
14
ModuleNotFoundError: No module named 'matplotlib'
You might be facing the problem of a double version of Python installed on your computer. Jupyter is trying to compile the Tensorflow tutorial file but with the wrong kernel (which should be Python 3).
The same happened to me when trying to run ipynb files in which Tensorflow was included.
So I would first recommend you to do the following:
Check the output of this command in your command line:
jupyter kernelspec list
Then it should output something similar to this: (in case of Windows OS)
python3 c:\python 3.6.8\share\jupyter\kernels\python3
If you find "python2" or any other type of version, you should remove it manually or try by using the command :
jupyter kernelspec remove python_wrong_version
Finally, if the problem is still there you can also check the link https://github.com/jupyter/notebook/issues/397 for further discussions.
I want to import Keras from the Tensorflow library. So I did the following thing :
import tensorflow.python.keras as keras
But this import throws the error :
AttributeError: module 'tensorflow' has no attribute 'python'
It appears that both the following import work correctly :
import tensorflow.python.keras
from tensorflow.python import keras
For me, import tensorflow.python.keras as keras and from tensorflow.python import keras are identical but it look like it's not true. What is the difference between these two imports ?
You should try something like this:
import tensorflow as tf
import tensorflow
from tensorflow import keras
from keras.layers import Dense
# ...
The first line helps you use the tensorflow module with the tf short name (asname).
For the asname you can check this thread.
I have installed keras followed by tensorflow. When I execute keras sequential model, I get an error message stating that
from keras.models import sequential
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'sequential'
Changing my comment to the answer.
Try Sequential with capital S
The problem was the lower case s in Sequential when you imported it. It should have been - from keras.models import Sequential.However, as a side note, you can use the code from tensorflow.keras import Sequential, using the tensorflow keras api 😊. If you are using colab, you can write %tensorflow_version 2.x at the beginning of your notebook to use version 2 of tensorflow
I am having trouble getting tensorflow to work using Jupiter notebooks. I am a complete noob so please keep responses as simple as possible and apologies if this is trivial.
I run the following code in the notebook:
import tensorflow as tf
from tensorflow.python.framework import ops
And get this error message:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-15-509396287076> in <module>()
1
2 import tensorflow as tf
----> 3 from tensorflow.python.framework import ops
4 from tf_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict
5
ModuleNotFoundError: No module named 'tensorflow.python'
(I originally struggled to get the "import tensorflow as tf" line to work but have resolved that. The code runs fine without the second line...)
Thanks for your help!