You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu系统Python3.6中flow_from_directory报错不存在且不接受参数

Troubleshooting flow_from_directory not found error in Ubuntu + Python 3.6

Hey there! Let's break down why you're seeing the "Can't use 'flow_from_directory' because it doesn't seems to exist" error, and how to fix it. This is a common gotcha when starting out with image data generators in Python, so don't worry—we'll get you sorted.

Common Causes & Fixes

1. You're not calling the method on an ImageDataGenerator instance

flow_from_directory isn't a standalone function—it's a method of the ImageDataGenerator class (part of Keras/TensorFlow Keras). If you're trying to call it directly like flow_from_directory(...) instead of first creating an instance of the generator, Python won't recognize it.

Here's the correct way to use it:

# First import the right class
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Or if you're using standalone Keras: from keras.preprocessing.image import ImageDataGenerator

# Create an instance of ImageDataGenerator
datagen = ImageDataGenerator(rescale=1./255)  # Add any preprocessing params you need

# Now call flow_from_directory on the instance
train_generator = datagen.flow_from_directory(
    'path/to/your/train/directory',
    target_size=(150, 150),  # Adjust to your image size
    batch_size=32,
    class_mode='binary'  # Use 'categorical' for multi-class tasks
)

2. You've imported the wrong library or class

Double-check your import statement. If you're importing from a different module (like a custom data generator or another library), the class won't have the flow_from_directory method. Make sure you're importing exactly:

  • from tensorflow.keras.preprocessing.image import ImageDataGenerator (for TensorFlow-integrated Keras)
  • OR from keras.preprocessing.image import ImageDataGenerator (for standalone Keras)

3. Your TensorFlow/Keras version is incompatible with Python 3.6

Python 3.6 is an older version, so you need to use a compatible release of TensorFlow/Keras:

  • TensorFlow versions up to 2.5.x support Python 3.6 (versions 2.6+ drop Python 3.6 support)
  • Standalone Keras versions up to 2.4.3 work with Python 3.6

To check your current version:

pip show tensorflow  # For TensorFlow Keras
# OR
pip show keras  # For standalone Keras

If you have an incompatible version, install a compatible one with:

pip install tensorflow==2.5.0 --user
# OR
pip install keras==2.4.3 --user

4. You might have a typo in the method name

Double-check that you're writing flow_from_directory correctly—capitalization and underscores matter! It's easy to mistype as flow_from_dir or FlowFromDirectory, which would cause the "doesn't exist" error.

Next Steps if You're Still Stuck

If none of the above fixes work, share a snippet of your code (the import line and the part where you try to use flow_from_directory)—that will help pinpoint exactly what's going wrong.

内容的提问来源于stack exchange,提问作者Pierre Riedinger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:35:01