Python 3.7.3安装Keras失败求助:是否仅支持3.6及以下版本?
Hey there! I totally get the frustration when you're just starting out with Python and hit roadblocks like this. Let's break down how to solve your Keras installation problem step by step.
First, let's confirm the root cause
You're right that early versions of Keras didn't support Python 3.7.x, but newer releases do have compatibility. The error you're seeing is likely because either:
- You're trying to install an outdated Keras version that doesn't work with 3.7.3
- Your pip version is too old to handle the compatibility checks properly
Here are your best solutions:
1. Install a Keras version compatible with Python 3.7.3
You can explicitly install a version that supports 3.7. Keras 2.2.5 and later work with Python 3.7. Run this command in your terminal:
pip install --upgrade pip pip install keras>=2.2.5
Upgrading pip first ensures you have the latest package manager to handle the installation smoothly.
2. Use TensorFlow's integrated Keras (highly recommended)
Nowadays, Keras is officially integrated into TensorFlow, and TensorFlow has excellent support for Python 3.7. Instead of installing Keras separately, install TensorFlow and use its built-in Keras module:
pip install tensorflow
Then replace your import line with:
import tensorflow.keras as ks
This is the most future-proof approach since TensorFlow actively maintains its Keras implementation.
3. Switch to a Python 3.6 virtual environment
If you need to stick with an older Keras version that only supports Python 3.6, create a virtual environment to isolate your setup:
- Using Conda (if you have Anaconda/Miniconda installed):
conda create -n py36_keras python=3.6 conda activate py36_keras pip install keras - Using virtualenv:
pip install virtualenv virtualenv -p python3.6 py36_keras # Activate on Windows: py36_keras\Scripts\activate # Activate on macOS/Linux: source py36_keras/bin/activate pip install keras
This way, you can keep your main Python 3.7.3 environment intact while using Python 3.6 for Keras work.
After trying any of these methods, test your import again—you should no longer see those lengthy error messages.
内容的提问来源于stack exchange,提问作者pat

