如何摆脱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 pythonorwhereis python3to 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:
This tells you which Python your shell uses by default.python --version && which python python3 --version && which python3
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:
Look for packages tagged withdpkg -l | grep pythonpython2.7orpython3.5—these are system-critical. - Track running Python processes: See which Python versions are currently executing with:
The path next to each process (e.g.,ps aux | grep python/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/binwith:
This will show you which scripts are hardcoded to use specific Python paths.grep -r "#\!/.*python" /path/to/your/scripts --include="*.py" --include="*.sh" - Audit virtual environments: If you use
virtualenv/venv, environments are usually in~/.virtualenvsor project-specificvenv/envfolders. Check their last modified time to spot abandoned ones:
If an environment hasn’t been touched in months, it’s likely unused—but double-check if any projects still point to it.ls -la ~/.virtualenvs/
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:
If no critical tools rely on it, remove it completely:apt-cache rdepends python3.6sudo apt remove --purge python3.6 sudo apt autoremove - Manually remove non-system installs: For Python executables in
/usr/local/binor custom paths, confirm they’re unused first withlsof /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
pyenvto manage multiple Python versions in isolation. Install its dependencies first:
Then clone the repo and set up your shell config (add the necessary lines tosudo 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~/.bashrcto 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.txtso you can restore them if needed. - Double-check before deleting: Always confirm a Python version isn’t in use with
ps aux | grep pythonorlsofbefore removing it.
内容的提问来源于stack exchange,提问作者Gabriel Fair
相关产品推荐
相关产品推荐

