Ubuntu 17.10下Python 2.7虚拟环境无法安装包的问题排查
Hey there! Let's walk through the common pitfalls and fixes when you're trying to set up a Python 2.7 virtual environment alongside an Anaconda Python 3.6.3 installation. Here's what to check and fix:
1. Ensure virtualenv is installed for Python 2.7
If you originally installed virtualenv using Anaconda's Python 3 pip, it might be tied to Python 3 by default, which can cause conflicts when targeting Python 2.7.
First, check which
virtualenvyou're using:which virtualenvIf the path points to your Anaconda
bindirectory (like~/anaconda3/bin/virtualenv), it's Python 3-based.Fix this by installing
virtualenvspecifically for Python 2.7 using your system's Python 2.7pip:/usr/bin/python2.7 -m pip install virtualenv(Replace
/usr/bin/python2.7with the actual path fromwhich python2.7if it's different.)
2. Double-check your virtualenv creation command
Make sure you're using the correct, full path to your system's Python 2.7 executable.
- First confirm the path:
which python2.7 - Then create the virtual environment with that exact path:
(Swapvirtualenv --python=/usr/bin/python2.7 /path/to/your/project/dir/path/to/your/project/dirwith your actual project folder path.)
3. Verify the activated environment is using Python 2.7
After activating the environment, always confirm you're in the right Python version:
- Activate the environment (Linux/macOS):
source /path/to/your/project/dir/bin/activate - Check versions:
python --version # Should show Python 2.7.x pip --version # Should show pip tied to Python 2.7
If these show Python 3, your virtualenv wasn't created correctly—go back to step 2 and ensure you used the right Python 2.7 path.
4. Fix outdated pip for Python 2.7
Python 2.7 is end-of-life, and newer pip versions don't support it. If you get errors like SyntaxError or "pip is too old", upgrade pip to the last version compatible with Python 2.7:
pip install pip==20.3.4
5. Resolve package installation errors
Many modern packages no longer support Python 2.7. If you get errors installing a package, look for the latest version that explicitly supports Python 2.7:
- For example, to install a compatible version of
requests:pip install requests==2.27.1 - Check the package's documentation or PyPI page to find the last Python 2.7-compatible release.
6. Isolate the environment from Anaconda
If Anaconda's packages are still leaking into your virtual environment, create the env with the --no-site-packages flag to fully isolate it:
virtualenv --python=/usr/bin/python2.7 --no-site-packages /path/to/your/project/dir
内容的提问来源于stack exchange,提问作者Dhruv Marwha

