Ubuntu 16.04下同步服务器与本地Python依赖版本失败求助
Hey Mike, I’ve run into this exact same headache before—even matching OS versions don’t always save you from pip version conflicts. Let’s walk through actionable steps to get your dependencies synced up:
1. Verify Python/Pip Version Alignment First
First things first: make sure both your server and local machine are using the exact same Python and pip versions. Ubuntu 16.04 ships with both Python 2.7 and 3.5 by default, so it’s easy to mix them up.
Run these commands on both systems and compare the outputs:
# Check Python version python --version python3 --version # Check pip version pip --version pip3 --version
If they don’t match, adjust your local setup to match the server (e.g., use pip3 instead of pip if the server runs Python 3).
2. Regenerate the Requirements File Correctly
Sometimes the issue starts with how you exported dependencies on the server. Make sure you’re using the same pip tied to your project’s Python version to generate the file:
# For Python 3 (most likely your case) pip3 freeze > requirements.txt # For Python 2 (if that's what your project uses) pip freeze > requirements.txt
If you’re using a virtual environment on the server, activate it first before running freeze—don’t use a system-wide pip for this.
3. Use a Virtual Environment Locally (Critical!)
Never install packages system-wide for development—it’s a surefire way to hit version conflicts. Create and activate a virtual environment that matches your server’s Python version:
# Create a Python 3.5 venv (Ubuntu 16.04's default Python 3) python3 -m venv my_project_env # Activate it source my_project_env/bin/activate
Once activated, your terminal will show the venv name at the start of the prompt. Now install the dependencies:
pip install -r requirements.txt
4. Troubleshoot Specific Version Conflicts
If you still get errors like "Could not find a version that satisfies the requirement", dig into the problematic package:
- Check the server’s installed version with
pip3 show <package-name>(e.g.,pip3 show Django) and hardcode that exact version in yourrequirements.txt(e.g.,Django==2.2.28—since Django 2.2 is the last series compatible with Python 3.5). - For packages relying on system libraries (like
mysqlclientorpsycopg2), install the required Ubuntu dependencies locally first:sudo apt-get install python3-dev libmysqlclient-dev # For mysqlclient sudo apt-get install python3-dev libpq-dev # For psycopg2
5. Force Install Compatible Versions (Last Resort)
If all else fails, you can tell pip to ignore minor conflicts and install the closest compatible versions (use this cautiously, as it might break edge-case functionality):
pip install -r requirements.txt --upgrade --force-reinstall
内容的提问来源于stack exchange,提问作者Mike75

