You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何摆脱Python环境混乱?Ubuntu 16.04多版本清理求助

Hey there, let's tackle this messy Python version situation on your Ubuntu 16.04 step by step—this is super common once you start tinkering with packages or virtual environments, so you’re definitely not alone in this!

1. First, map out all Python installations on your system

Start by getting a full list of every Python executable and related files:

  • Run whereis python or whereis python3 to quickly see binary, library, and documentation paths for Python 2/3.
  • For a more comprehensive scan (ignoring permission errors), use:
    find / -name "python*" -type f -executable 2>/dev/null
    
  • Check your default Python versions and their paths with:
    python --version && which python
    python3 --version && which python3
    
    This tells you which Python your shell uses by default.
2. Identify which Python environments are actively in use

Not all installations are equal—some are critical for the system, others are tied to your projects. Here’s how to spot the active ones:

  • Check system dependencies: Ubuntu 16.04 relies on Python 2.7 and Python 3.5 for core tools like apt. Never delete these! List all apt-installed Python packages with:
    dpkg -l | grep python
    
    Look for packages tagged with python2.7 or python3.5—these are system-critical.
  • Track running Python processes: See which Python versions are currently executing with:
    ps aux | grep python
    
    The path next to each process (e.g., /home/you/.virtualenvs/myapp/bin/python3.6) tells you which environment is active.
  • Search for script shebangs: Many scripts specify a Python version in their first line (the shebang). Scan your project directories, ~/bin, or /usr/local/bin with:
    grep -r "#\!/.*python" /path/to/your/scripts --include="*.py" --include="*.sh"
    
    This will show you which scripts are hardcoded to use specific Python paths.
  • Audit virtual environments: If you use virtualenv/venv, environments are usually in ~/.virtualenvs or project-specific venv/env folders. Check their last modified time to spot abandoned ones:
    ls -la ~/.virtualenvs/
    
    If an environment hasn’t been touched in months, it’s likely unused—but double-check if any projects still point to it.
3. Spot abandoned Python directories

Compare the full list of Python paths from step 1 with the active ones from step 2. Here’s how to categorize what’s safe to remove:

  • Leave /usr/bin/ system Python alone: Only touch versions here if you explicitly installed them via apt (e.g., Python 3.6) and confirmed no system tools depend on them.
  • Audit /usr/local/bin/: Versions here are usually manually compiled or installed via pip. If you can’t find any processes, scripts, or projects referencing them, they’re probably safe to delete.
  • Virtual environments: If you’re certain no project uses a virtual environment anymore, just delete the entire folder (e.g., rm -rf ~/.virtualenvs/old_env).
4. Safe cleanup tools & strategies
  • Clean apt-installed versions: If you added a Python version via apt (like Python 3.6), first check for dependent packages:
    apt-cache rdepends python3.6
    
    If no critical tools rely on it, remove it completely:
    sudo apt remove --purge python3.6
    sudo apt autoremove
    
  • Manually remove non-system installs: For Python executables in /usr/local/bin or custom paths, confirm they’re unused first with lsof /path/to/python (checks for open processes), then delete them:
    sudo rm /usr/local/bin/python3.6
    sudo rm -rf /usr/local/lib/python3.6
    
  • Prevent future mess with pyenv (optional): To avoid version conflicts down the line, use pyenv to manage multiple Python versions in isolation. Install its dependencies first:
    sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
    
    Then clone the repo and set up your shell config (add the necessary lines to ~/.bashrc to load pyenv on startup).
Critical Warnings
  • Never delete system default Python 2.7/3.5: Ubuntu 16.04’s core tools depend on these—removing them will break your system.
  • Backup first: Save dependencies for active virtual environments with pip freeze > requirements.txt so you can restore them if needed.
  • Double-check before deleting: Always confirm a Python version isn’t in use with ps aux | grep python or lsof before removing it.

内容的提问来源于stack exchange,提问作者Gabriel Fair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:56:41