从TensorFlow导入MNIST数据集遇SSL证书错误(Anaconda环境)求助
Hey there, let's work through this SSL certificate issue and get your MNIST dataset loaded properly. First, let's address a couple of key points that might be causing your trouble:
1. You might have installed the wrong package (not certified but certifi)
You mentioned uninstalling/reinstalling certified, but the correct Python package for managing SSL certificates is certifi. That's likely a typo that's been holding you back. Run these commands to fix it:
pip uninstall certified pip install --upgrade certifi
2. The tensorflow.examples.tutorials.mnist module is deprecated
This module is no longer maintained in newer TensorFlow versions (especially TF 2.x), which can lead to unexpected errors including SSL issues. The recommended approach is to use the built-in Keras dataset loader instead—it's more reliable and avoids these legacy problems:
import tensorflow as tf # Load MNIST directly via Keras (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # If you need the same structure as input_data (like one-hot labels), you can convert it: y_train = tf.keras.utils.to_categorical(y_train, 10) y_test = tf.keras.utils.to_categorical(y_test, 10)
3. If you must use the legacy input_data module (TF 1.x only)
If you're stuck using TensorFlow 1.x and the old module, here are two ways to bypass the SSL error:
Option A: Disable SSL verification (temporary fix)
This skips certificate checks for the download. Note: This isn't secure for production, but works for testing:
import ssl # Disable SSL verification ssl._create_default_https_context = ssl._create_unverified_context from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("tmp/data/", one_hot=True)
Option B: Manually download the dataset
Download the MNIST files directly and place them in your tmp/data/ folder. The required files are:
train-images-idx3-ubyte.gztrain-labels-idx1-ubyte.gzt10k-images-idx3-ubyte.gzt10k-labels-idx1-ubyte.gz
When you run read_data_sets, it will detect the local files and skip the download, avoiding the SSL error entirely.
内容的提问来源于stack exchange,提问作者Prashanth Pradeep

