Checking Current Python VersionBefore updating Python, verify the installed versions on your CentOS system using:

python --version# Checks Python 2.x (if installed)python3 --version # Checks Python 3.x (default in CentOS 7+)This helps determine whether an update is needed and identifies the current default version.
Updating System PackagesEnsure your system’s package index is up-to-date to avoid compatibility issues during Python installation:
sudo yum update -y# For CentOS 7sudo dnf update -y# For CentOS 8+This step installs the latest security patches and dependencies required for Python.
Installing/Updating Python via Yum/DNF (Recommended for Stability)The easiest way to install or update Python is using CentOS’ native package manager. This method ensures compatibility with system tools but may not provide the latest Python versions.
sudo yum install epel-release -y# Enable EPEL for additional packagessudo yum install python3 -y # Installs/updates Python 3 (e.g., 3.6)sudo dnf install epel-release -ysudo dnf install python3 -y # Installs/updates Python 3 (e.g., 3.9)After installation, verify the version:
python3 --versionTo set Python 3 as the default python command (optional), use:
sudo alternatives --install /usr/bin/python python /usr/bin/python3 1sudo alternatives --config python # Select Python 3 from the listInstalling/Updating Python from Source (For Latest Versions)If you need a specific Python version (e.g., 3.11) or custom optimizations, compile from source. This method requires manual dependency management but offers flexibility.
sudo yum groupinstall "Development Tools" -y# Compilers, make, etc.sudo yum install openssl-devel bzip2-devel libffi-devel wget -y# Python build dependenciescd /usr/src# Recommended directory for source filessudo wget https://www.python.org/ftp/python/3.11.6/Python-3.11.6.tgzsudo tar xzf Python-3.11.6.tgzcd Python-3.11.6sudo ./configure --enable-optimizations# Optimizes Python performancesudo make altinstall # Avoids overwriting system Python (installs as python3.11)Note: Use altinstall instead of install to prevent conflicts with the system’s default Python.python3.11 --version# Replace with your installed versionManaging Multiple Python Versions with pyenvFor users needing multiple Python versions (e.g., 3.8 for legacy apps, 3.11 for new projects), pyenv is a powerful tool. It allows easy switching between versions without affecting system stability.
curl https://pyenv.run | bashAdd the following to your ~/.bashrc (or ~/.zshrc) to enable pyenv:export PATH="$HOME/.pyenv/bin:$PATH"eval "$(pyenv init --path)"eval "$(pyenv virtualenv-init -)"Reload the shell:source ~/.bashrcpyenv install 3.11.6# Installs Python 3.11.6pyenv global 3.11.6 # Sets as the default Python for the userVerify with:python --version# Should show 3.11.6To install a specific version for a project (without changing the global default):cd /path/to/projectpyenv local 3.8.12# Creates a .python-version file for the projectCreating Virtual Environments (Best Practice for Isolation)After updating Python, always use virtual environments to isolate project dependencies and avoid conflicts.
sudo yum install python3-venv -ypython3 -m venv myenv# Creates a virtual environment named 'myenv'source myenv/bin/activate# Activates the environment (changes prompt to (myenv))Once activated, install packages using pip (e.g., pip install numpy). Deactivate with deactivate when done.Key Notes for a Safe Update
/etc, /home).yum). Always use altinstall or tools like pyenv to prevent breakage.DeepSeek多模态怎么样及保姆级配置教程(codex、claude)实用指南
WSL2+Docker+Ollama本地部署大模型思路和实践指南(Qwen1.8b)实用指南
DeepSeek Harness热门插件盘点:10个实用插件,覆盖多Agent、上下文和多模态实用指南
grep文本搜索与正则表达式完整代码示例实用指南
AI漫剧伪装直男1‑24集无雾版夸克网盘资源免费分享
AI漫剧玉僧入尘1-68集无雾无删版夸克网盘资源免费分享