EC2实例中Python3环境pip安装报错及pip缺失问题求助
Hey there, let's work through your two issues step by step:
1. Resolving "Network is unreachable" when using pip in virtualenv
This error almost always ties back to EC2's network restrictions. Here's what to check and fix:
- Security Group Outbound Rules: Jump into the EC2 console, locate your instance's security group, and confirm the outbound rules allow traffic to
0.0.0.0/0on ports 80 (HTTP) and 443 (HTTPS). Some default security groups lock down outbound traffic, which blocks pip from connecting to PyPI servers. - Test Basic Network Connectivity: Run
curl https://pypi.orgin your instance's terminal. If this fails, double-check your subnet's route table—make sure it's linked to an internet gateway. If you're using a private subnet, you'll need a NAT gateway configured to access the public internet. - Check Proxy Settings: If your environment uses a proxy, verify pip isn't picking up incorrect proxy configs. After activating your virtualenv, run
pip config listto see if there's an unwantedproxyentry. Remove it withpip config unset global.proxyif needed.
2. Fixing "/usr/bin/python3: No module named pip"
Since you manually installed Python 3.5.1 on an EC2 instance that defaults to Python 2.7, pip3 wasn't included automatically. Here's how to install it:
For Amazon Linux/CentOS-based instances:
- First, install required system dependencies:
sudo yum install -y python3-devel - Download the legacy pip bootstrap script compatible with Python 3.5 (note: Python 3.5 is end-of-life, so we need the specific version of the script):
curl https://bootstrap.pypa.io/pip/3.5/get-pip.py -o get-pip.py - Install pip3 for your system-wide Python 3:
sudo python3 get-pip.py - Verify the installation worked:
python3 -m pip --version
Now you should be able to run sudo python3 -m pip install Flask without errors.
Quick Virtualenv Best Practice
After creating your virtualenv with virtualenv -p python3 .env, don't forget to activate it first:
source .env/bin/activate
Once activated, use pip (not pip3)—the virtualenv's pip is already linked to your Python 3 installation. Then run pip install -r requirements.txt once your network issues are sorted.
内容的提问来源于stack exchange,提问作者jason

