安装TensorFlow 1.x调用tensorflow.contrib遇Python版本问题求助
Hey there, let's work through this issue step by step — I've run into similar environment mismatch headaches before, so I know exactly where you might be hitting snags.
核心问题分析
When you ran conda install python==3.7 to downgrade, it likely didn't fully update your active Python environment (or you weren't using the right environment at all). This is why your python.exe was still reporting version 3.8 when you tried installing TensorFlow 1.15. Base environments in Anaconda can be finicky to modify directly, so the cleanest fix is to create a fresh, isolated environment.
Step 1: Create & Activate a Python 3.7 Conda Environment
Let's start from scratch with a dedicated environment for TensorFlow 1.x:
- Open Anaconda Prompt (or your terminal of choice) and run this to create a new environment named
tf1_envwith Python 3.7:conda create -n tf1_env python=3.7 - Once it's created, activate the environment to make it your active working space:
conda activate tf1_env - Verify the Python version to be sure:
You should see something likepython --versionPython 3.7.16— if that's the case, you're ready to move on.
Step 2: Install TensorFlow 1.15 in the New Environment
With the correct environment active, install TensorFlow 1.15 using pip (it's the most reliable way for TF 1.x versions):
pip install tensorflow==1.15
After installation, test the import to confirm it works:
import tensorflow as tf from tensorflow.contrib import learn print(tf.__version__) # Should output 1.15.x
This should resolve the import error since TensorFlow 1.15 is fully compatible with Python 3.7.
Why Your Previous Downgrade Failed
A couple of common pitfalls might have tripped you up:
- Base environment dependency conflicts: The base Anaconda environment has pre-installed packages that may rely on Python 3.8, so downgrading it often doesn't fully replace the Python version.
- Pip path mismatch: Even if conda tried to downgrade Python, your system's pip might still be pointing to the old Python 3.8 installation, leading to version mismatch errors during TF installation.
Alternative Installation Method: Conda-Forge
If you prefer using conda over pip, you can install TensorFlow 1.15 directly via conda-forge (since the default conda repo might not have the older TF version readily available):
conda activate tf1_env conda install -c conda-forge tensorflow==1.15
Conda will handle all dependency matching automatically, so you don't have to worry about version conflicts.
Quick Tips to Avoid Future Environment Issues
- Never modify the base environment: Always create separate environments for different projects — this keeps dependencies isolated and prevents messy conflicts.
- Double-check your active environment: The name of your active environment will show up in parentheses at the start of your terminal prompt (e.g.,
(tf1_env)). Always confirm this before installing packages.
内容的提问来源于stack exchange,提问作者Ashten




