如何在macOS系统中设置默认Python版本与pip?
Hey there, let's get this sorted out—setting your default Python to 3.8.2 and syncing pip doesn't have to be a headache. Here's what's going on and how to fix it:
Why Your Current Setup Isn't Working
First off, if you're on macOS Catalina or later, the default shell is zsh, not bash. That means any changes you make to .bash_profile won't take effect unless you explicitly use bash. Even if you were using bash, modifying PATH alone might not override the system's default python alias which points to Python 2.7.
Step-by-Step Solution
Let's go with the safest approach (we won't mess with system-level files that could break macOS tools):
Check your active shell
Run this command to confirm which shell you're using:echo $SHELLYou'll see either
/bin/bashor/bin/zsh.Edit the right configuration file
- If you're using bash: Open
.bash_profilewith a text editor:nano ~/.bash_profile - If you're using zsh: Open
.zshrcinstead:nano ~/.zshrc
- If you're using bash: Open
Add aliases for Python and pip
Scroll to the end of the file and paste these two lines—they'll makepythonandpippoint to your desired 3.8.2 versions:alias python="/usr/bin/python3" alias pip="/usr/bin/pip3"Save and exit the editor
In nano, pressCtrl+Oto save, hit Enter to confirm the filename, thenCtrl+Xto exit.Apply the changes immediately
- For bash:
source ~/.bash_profile - For zsh:
source ~/.zshrc
- For bash:
Verify the fix
Run these commands to check if everything works:python -V # Should return Python 3.8.2 pip -V # Should show it's linked to Python 3.8.2
Important Note
Avoid modifying the system's default python symlink in /usr/bin—macOS relies on Python 2.7 for some built-in tools, and changing this could cause unexpected issues. Using aliases is the safest way to set your preferred Python version without breaking system functionality.
Once this is set up, Spyder IDE should automatically pick up the default Python version you've configured. If it doesn't, you can manually set the interpreter path in Spyder's settings (go to Preferences > Python interpreter and select /usr/bin/python3).
内容的提问来源于stack exchange,提问作者Augusto




