如何在同一段代码中分别使用Keras 2.4.3、2.3.0和2.1.6版本?
First, let's clear up a key point: when you run pip install keras==x.x.x multiple times, each subsequent installation replaces the previous one. Right now, your global Python environment only has the last version you installed (2.1.6) — the earlier 2.4.3 and 2.3.0 copies were overwritten. To use all three versions, you need to either isolate them in virtual environments (the recommended approach) or install them to separate directories and dynamically load them in your code.
Method 1: Virtual Environments (Recommended)
Virtual environments keep package versions completely isolated, so you won't run into messy dependency conflicts. Here's how to set this up with Python's built-in venv module:
Create three separate virtual environments:
# Env for Keras 2.4.3 python -m venv keras_243_env # Env for Keras 2.3.0 python -m venv keras_230_env # Env for Keras 2.1.6 python -m venv keras_216_envActivate each environment and install the matching Keras version:
For Windows (Command Prompt):keras_243_env\Scripts\activate pip install keras==2.4.3 deactivate keras_230_env\Scripts\activate pip install keras==2.3.0 deactivate keras_216_env\Scripts\activate pip install keras==2.1.6 deactivateFor macOS/Linux:
source keras_243_env/bin/activate pip install keras==2.4.3 deactivate source keras_230_env/bin/activate pip install keras==2.3.0 deactivate source keras_216_env/bin/activate pip install keras==2.1.6 deactivateUse the versions in your workflow:
Split your code into three scripts (one for each Keras version) and run each with the corresponding environment's Python interpreter:# Run code with Keras 2.4.3 keras_243_env/bin/python your_243_code.py # Run code with Keras 2.3.0 keras_230_env/bin/python your_230_code.py # Run code with Keras 2.1.6 keras_216_env/bin/python your_216_code.pyIf you use Jupyter Notebook, add each environment as a kernel to switch versions directly in your notebook:
source keras_243_env/bin/activate pip install ipykernel python -m ipykernel install --user --name=keras_243_env deactivateRepeat this for the other two environments, then select the desired kernel from Jupyter's "Kernel" menu.
Method 2: Dynamic Loading (Single Environment)
If you prefer to work in one environment, install each Keras version to a dedicated directory and dynamically load them in your code. Note: This can cause dependency conflicts (e.g., different TensorFlow versions required by Keras), so proceed carefully.
Install each Keras version to a unique directory:
pip install keras==2.4.3 --target=./keras_243 pip install keras==2.3.0 --target=./keras_230 pip install keras==2.1.6 --target=./keras_216Dynamically switch versions in your code:
Usesys.pathto prioritize the target directory for the version you want, then clean up modules before switching:import sys import importlib # Load and use Keras 2.4.3 sys.path.insert(0, './keras_243') import keras print(f"Current Keras version: {keras.__version__}") # Add your Keras 2.4.3-specific code here... # Clean up to switch versions del keras if 'keras' in sys.modules: importlib.reload(sys.modules['keras']) del sys.modules['keras'] sys.path.pop(0) # Load and use Keras 2.3.0 sys.path.insert(0, './keras_230') import keras print(f"Current Keras version: {keras.__version__}") # Add your Keras 2.3.0-specific code here... # Clean up again del keras if 'keras' in sys.modules: importlib.reload(sys.modules['keras']) del sys.modules['keras'] sys.path.pop(0) # Load and use Keras 2.1.6 sys.path.insert(0, './keras_216') import keras print(f"Current Keras version: {keras.__version__}") # Add your Keras 2.1.6-specific code here...
内容的提问来源于stack exchange,提问作者gangadharK

