无法在PyCharm中从tf.keras.datasets导入示例数据集。

huangapple go评论69阅读模式
英文:

Cannot import example datasets from tf.keras.datasets in PyCharm

问题

我正在尝试使用以下代码导入MNIST数据集:

from tensorflow.python.keras.datasets import mnist

然而,我遇到以下错误:

ModuleNotFoundError: No module named 'tensorflow.python.keras.datasets'

我已经通过pip在我的PyCharm项目的虚拟环境中安装了tensorflow和keras v2.12.0。来自tensorflow的其他导入都正常工作。我可以导入模型、层等等。然而,tensorflow v2.12.0的文档网站指示我应该能够访问示例数据集。我参考了这个页面:https://www.tensorflow.org/api_docs/python/tf/keras/datasets

我还看到了这个站点和其他站点上的示例代码,它们使用以下语法导入数据集:

from tensorflow.keras.datasets import mnist

但这也不起作用。我对我的导入为什么看起来不同,并且为什么只允许我访问文档页面中列出的一些库而不是所有库感到困惑。我使用的是pip v23.1.2和python 3.11.0。我没有安装其他版本的python。此外,我正在使用PyCharm Professional 2023.1.2。我不确定这是否是一个PyCharm特定的问题,如果有人有解决方案,请告诉我。

当我检查/venv/Lib/site-packages/tensorflow/python文件夹时,keras被列出,但数据集却不见了。

期望使用以下导入语句从keras.datasets中导入示例数据集:

from tensorflow.python.keras.datasets import mnist

但是实际上得到了错误:

ModuleNotFoundError: No module named 'tensorflow.python.keras.datasets'

来自tensorflow的其他导入工作正常,例如:

from tensorflow.python.keras.models import Sequential
英文:

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

答案1

得分: 0

我找到了解决方法。我认为这是PyCharm及其包管理器的问题,所以这是我的解决方法。
在我的项目目录中,我删除了PyCharm创建的venv。
我在项目目录中打开了一个命令终端,并使用python -m venv ./venv创建了一个新的环境。
我使用./venv/Scripts/Activate.ps1启动了venv。
然后,我使用pip3 install tensorflow将tensorflow安装到这个venv中。
我通过在终端中运行内联Python脚本来验证tensorflow是否正确安装:

如果包已正确安装,它将导入我正在寻找的数据集并打印其形状为(60000, 28, 28),并且没有错误,因此我们已正确安装了tensorflow!

接下来,我打开了PyCharm,并从菜单中导航到File -> Settings或(Ctrl+Alt+S)。在搜索栏中,我搜索了'python interpreter',解释器的设置会自动弹出在右侧。

从终端创建的venv应该出现在顶部的Python解释器下拉菜单中。选择它,然后在底部点击'Apply'和'OK'。
做完这些步骤后,我的所有导入都可以正常工作。代码编辑器仍会在导入下划线并警告找不到包,但运行时一切都正常。我之前运行了内联测试,但将代码保存为文件,并从PyCharm中运行它。不幸的是,自动完成不起作用,并且某些模块的参数提示不显示,但至少它能正常工作。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月29日 14:47:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定