如何在Mac OS X上将Python 3设为默认版本?解决Django安装兼容问题
Hey there! Let's get your Python 3.6 set as the system default so you can install Django 2.0.4 without hitting that Python 2.7 limitation. The approach varies a bit depending on your operating system, so I'll break it down for both Linux/macOS and Windows:
For Linux or macOS
Method 1: Use update-alternatives (Debian/Ubuntu-based systems)
This is the most reliable way to set a system-wide default Python version:
First, check the paths of your Python installations (if you don't know them already):
which python2.7 which python3.6Register both versions with the alternatives system. The number at the end is the priority—higher numbers mean higher priority:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2Now select Python 3.6 as the default:
sudo update-alternatives --config pythonYou'll see a list of available versions—type the number next to Python 3.6 and press Enter.
Verify the change works:
python --versionIt should show
Python 3.6.x.To fix
piptoo (so it uses Python 3 by default), repeat the process for pip:sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip2 1 sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 2 sudo update-alternatives --config pip
Method 2: Adjust shell configuration (for macOS or non-Debian systems)
If update-alternatives isn't available, you can modify your shell's startup file to set a persistent alias (I'm guessing you tried temporary aliases before—this makes it stick across all sessions):
- Open your shell config file (
.bashrcfor Bash,.zshrcfor Zsh) in a text editor:nano ~/.bashrc # or nano ~/.zshrc - Add these lines at the end:
alias python='/usr/bin/python3.6' alias pip='/usr/bin/pip3' - Save the file and reload the config:
source ~/.bashrc # or source ~/.zshrc - Verify with
python --versionandpip --version.
For Windows
- First, find your Python 3.6 installation folder (usually
C:\Python36orC:\Users\YourName\AppData\Local\Programs\Python\Python36). - Open System Properties: Right-click "This PC" → "Properties" → "Advanced system settings".
- Click Environment Variables.
- In the System variables section, find the
Pathvariable and click "Edit". - Move the entries for Python 3.6 (e.g.,
C:\Python36andC:\Python36\Scripts) above the entries for Python 2.7. - Click "OK" to save all changes.
- Important: Close any open Command Prompt/PowerShell windows and open a new one.
- Verify the change:
Both should now point to Python 3.6.python --version pip --version
Once you've set Python 3 as default, run pip install Django==2.0.4 again—it should work perfectly now!
内容的提问来源于stack exchange,提问作者yoursweater

