I am trying to import the MNIST dataset with the following:
from tensorflow.python.keras.datasets import mnist
However, I am getting the follow error
ModuleNotFoundError: No module named 'tensorflow.python.keras.datasets'
I have tensorflow and keras v2.12.0 installed via pip in my venv for the PyCharm project. Other imports from tensorflow work just fine. I can import models, layers, etc. However, the documentation website for tensorflow v2.12.0 indicates that I should be able to access the example datasets. I am referencing this page: https://www.tensorflow.org/api_docs/python/tf/keras/datasets
I have also seen example code on this site and on other sites which import the datasets with the following syntax:
from tensorflow.keras.datasets import mnist
but this does not work either. I am confused why my imports both look different and will only allow me to access some of the libraries listed in the docs page but not all of them. I am using pip v23.1.2 and python 3.11.0. I do not have any other versions of python installed. Also for I am using PyCharm Professional 2023.1.2. I am not sure if this is a PyCharm specific issue and if anyone has a solution please let me know.
When I check the /venv/Lib/site-packages/tensorflow/python folder, keras is listed but the datasets is missing.
Expected to import sample datasets from keras.datasets using the import statement:
from tensorflow.python.keras.datasets import mnist
instead got error:
ModuleNotFoundError: No module named 'tensorflow.python.keras.datasets'
Other imports from tensorflow work fine, such as:
from tensorflow.python.keras.models import Sequential
I found a solution to this. I think its an issue with PyCharm and its package manager so here is my workaround.
In my project directory, I deleted the venv created by PyCharm.
I opened up a command terminal in my project directory and created a new environment using python -m venv ./venv
.
I launched the venv using ./venv/Scripts/Activate.ps1
.
Then I installed tensorflow to this venv using pip3 install tensorflow
.
I verified that tensorflow was installed correctly by running an inline python script from the terminal:
python -c "import tensorflow as tf; from tensorflow.keras.datasets import mnist; (x_train, y_train), (x_test, y_test) = mnist.load_data(); print(x_train.shape);"
This will import the dataset I was looking for and print the shape if the packages were installed correctly. It prints out (60000, 28, 28)
with no errors so we have installed tensorflow correctly!
Next I opened up PyCharm and from the menu navigated to File -> Settings
or (Ctrl+Alt+S). In the search bar I looked for 'python interpreter' and the settings for the interpreter automatically pop up on the right.
The venv created from the terminal should appear in the drop down menu at the top for the Python Interpreter. Select it and then hit 'Apply' and 'OK' at the bottom. After doing this all my imports work correctly. The code editor still underlines the imports and warns that the packages could not be found, but when running everything functions correctly. I ran the inline test from before but saved the code as a file and ran it from PyCharm. Unfortunately, autocomplete doesn't work and the argument hints do not show for certain modules but at least its working.