You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在macOS系统中设置默认Python版本与pip?

Fixing Default Python Version on macOS for Spyder IDE

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):

  1. Check your active shell
    Run this command to confirm which shell you're using:

    echo $SHELL
    

    You'll see either /bin/bash or /bin/zsh.

  2. Edit the right configuration file

    • If you're using bash: Open .bash_profile with a text editor:
      nano ~/.bash_profile
      
    • If you're using zsh: Open .zshrc instead:
      nano ~/.zshrc
      
  3. Add aliases for Python and pip
    Scroll to the end of the file and paste these two lines—they'll make python and pip point to your desired 3.8.2 versions:

    alias python="/usr/bin/python3"
    alias pip="/usr/bin/pip3"
    
  4. Save and exit the editor
    In nano, press Ctrl+O to save, hit Enter to confirm the filename, then Ctrl+X to exit.

  5. Apply the changes immediately

    • For bash:
      source ~/.bash_profile
      
    • For zsh:
      source ~/.zshrc
      
  6. 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

火山引擎 最新活动