如何通过Anaconda切换Python 3.x/3.y版本?HDP集群多版本共存方案
Absolutely, running both Python 3.5 and 3.6 side-by-side with Anaconda is not only possible—it’s a standard practice for managing multiple Python versions without conflicts. Let’s walk through your questions and the general workflow:
Yes, 100%. Conda’s environment system is designed exactly for this scenario: each environment is an isolated sandbox with its own Python version, packages, and dependencies. This means your existing Python 3.5 setup (and any HDP-related integrations relying on it) will remain completely untouched while you set up and use Python 3.6 in a separate environment.
Since you’re working on an HDP cluster, just ensure you have the necessary permissions to create conda environments (most clusters allow user-specific environments in home directories, even if the base Anaconda is installed system-wide).
Assuming your current Python 3.5 is running in the default base conda environment (or a custom environment you’ve already set up), here’s how to add Python 3.6 and toggle between them:
First, create a dedicated Python 3.6 environment
Run this command in your cluster’s terminal:conda create -n py36 python=3.6If your team relies on specific packages (like
pyspark,numpy, orpandasfor HDP workflows), include them in the command to install them alongside Python 3.6:conda create -n py36 python=3.6 pyspark numpy pandasActivate the Python 3.6 environment
For Linux-based HDP clusters, use:conda activate py36If you’re using an older version of conda that doesn’t support
conda activate, use:source activate py36Once activated, run
python --versionto confirm you’re on Python 3.6.Switch back to Python 3.5
To return to your original Python 3.5 environment (e.g., thebaseenvironment), run:conda activate baseIf your Python 3.5 is in a custom-named environment (like
py35), replacebasewith that environment name.
This workflow applies to any pair of Python 3 versions, not just 3.5 and 3.6:
Create an environment for your target Python version
Replace<version>with your desired Python release (e.g.,3.7,3.9):conda create -n py<version> python=<version>Example:
conda create -n py38 python=3.8Activate the target environment
- Linux/macOS:
conda activate py<version> - Windows (Command Prompt/PowerShell):
conda activate py<version>
- Linux/macOS:
Verify the active version
Confirm you’re using the correct Python with:python --versionYou can also check the path to ensure it’s pointing to the conda environment:
# Linux/macOS which python # Windows where pythonSwitch to another environment
Simply activate the environment you want to use:conda activate <environment-name>This could be the
baseenvironment, or another custom environment you’ve created.
Quick Tips for HDP Clusters
- Avoid modifying the
baseconda environment’s Python version—this can break core Anaconda functionality and cause conflicts with HDP tools. - If your team needs to share a consistent environment, export the environment config:
Team members can then recreate the environment with:conda env export -n py36 > hdp_py36_env.ymlconda env create -f hdp_py36_env.yml - When submitting Spark jobs, specify the conda environment’s Python path to ensure Spark uses the correct version:
spark-submit --conf spark.pyspark.python=/home/your-user/anaconda3/envs/py36/bin/python your_script.py
内容的提问来源于stack exchange,提问作者HISI

