如何让Virtualenv Wrapper创建的虚拟环境仅包含Python3.0+版本
Hey, I’ve dealt with this exact problem before—let’s get your virtual environments to only use Python 3 and ditch Python 2 entirely:
1. Set Python 3 as the Default for virtualenvwrapper
First, we need to tell virtualenvwrapper to prioritize Python 3 by default. Open your ~/.bashrc file (or ~/.zshrc if you use Zsh) and add these two lines:
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv-3
Pro tip: If you’re not sure where your Python 3 or virtualenv-3 are installed, run which python3 and which virtualenv-3 in your terminal to get the correct paths.
After adding these lines, reload your shell configuration to apply the changes:
source ~/.bashrc
2. Force Python 3 for Individual Environments (Optional)
If you ever need to override the default for a specific environment (or just want to be extra explicit), use the -p flag when creating the environment to point directly to your Python 3 interpreter:
mkvirtualenv -p /usr/bin/python3 your_env_name
This guarantees that only Python 3 is installed in the new environment—Python 2 won’t be included at all.
3. Verify Your Environment is Python 3-Only
Once your environment is created, activate it and double-check the Python versions to confirm everything worked:
workon your_env_name python --version # This should show Python 3.x (e.g., Python 3.8.10) python2 --version # This should throw an error like "command not found"—exactly what we want!
Why This Happens in the First Place
By default, virtualenvwrapper might fall back to your system’s default Python interpreter (which could still be Python 2 on some older setups) if you don’t explicitly configure it to use Python 3. Adding those environment variables to your bashrc ensures it always uses Python 3 unless you intentionally specify otherwise.
内容的提问来源于stack exchange,提问作者Nabin Bogati

