Anaconda依赖与包冲突求助:能否实现多版本包共存?
Hey there, I totally get the frustration of having your Anaconda environment get messed up because of unexpected package upgrades/downgrades—been there, done that. The good news is: yes, you absolutely can have multiple package versions coexist, but the right way to do it is through Anaconda's virtual environments (the tool was built for exactly this kind of dependency chaos!).
Let me walk you through the solutions step by step:
1. Use Virtual Environments to Isolate Dependencies
This is the gold standard for avoiding environment corruption. Each virtual environment acts as a separate sandbox, so packages installed in one won't affect another.
- Create a new environment with your desired Python 3.6 version (matches your original setup):
conda create -n ml_project python=3.6 - Activate the environment (Windows 10 command):
conda activate ml_project - Now you can install packages like lightGBM, TensorFlow, Keras, etc., in this isolated space without worrying about breaking your base environment. For example:
conda install -c conda-forge lightgbm tensorflow=1.15 keras=2.2.5 bokeh - Need another setup with different package versions? Just create another environment:
conda create -n tf2_project python=3.6 tensorflow=2.3 keras=2.4 bokeh=2.0 - To save/restore environments (super useful for sharing or rebuilding):
Export the environment config:
Create from the config:conda env export > ml_project_env.ymlconda env create -f ml_project_env.yml
2. Prevent Unwanted Upgrades/Downgrades in an Environment
If you want to tweak an existing environment without letting conda auto-update dependencies, use these flags:
- Install packages without updating existing dependencies:
Note: This might throw a dependency conflict error if the package needs a specific version of another library—if that happens, a new virtual environment is the safer bet.conda install -c conda-forge packagename --no-update-deps - Pin specific package versions to lock them in:
You can create aconda-meta/pinnedfile in your environment directory and add lines like:
This tells conda never to upgrade these packages beyond the specified versions.python=3.6.* tensorflow=1.15.*
3. Can You Have Multiple Versions of the Same Package in One Environment?
Short answer: Not really, and you don't want to. Python's import system will only load one version of a package at a time, so having multiple versions installed will lead to confusing errors (e.g., importing a function from the wrong version).
If you absolutely need to use different versions of a package in the same project, you can use importlib to dynamically load specific versions, but this is hacky and not recommended. Virtual environments are the clean, maintainable solution here.
Quick Fix for Your Broken Current Environment
If your base environment is already damaged, you can try rolling it back to a previous working state:
conda list --revisions
Pick a revision number from the list that was working, then run:
conda install --revision <your-revision-number>
If that doesn't work, just create a new clean Python 3.6 environment and use that as your new default instead of the broken base.
内容的提问来源于stack exchange,提问作者Nathan Furnal

