Modify the default Python version on Mac

Computer model: MacBook Pro [Chip: Apple M2 Max. macOS: Ventura 13.3]

1. Delete redundant Python versions on Mac

solution:

  1. Determine the installed Python version

      View the current system version command: python --version

      List all installed Python versions: ls /usr/bin/python*

  2. Delete Python files

       Find the Python path: which python

       Removing a specific Python version: sudo rm -rf <Python file path>

  3. Delete Python environment variables

       Run this command in the terminal to edit the environment variable configuration file: open ~/.zshrc [Note: The shell of the new Mac system is zsh instead of bash]

       In the open file, delete the environment variable settings related to the deleted Python version.

  4. Verify deletion

       Run the command: python + version number. If it prompts zsh: command not found, it means that the Python version has been successfully deleted.

2. Solve the problem that pyenv cannot switch Python versions on Mac

   1. Add pyenv environment

       Install pyenv: brew update

                        brew install peen

       Check whether pyenv is installed correctly: pyenv --version

       View all Python versions installed on the system: pyenv versions

       Install a specific Python version: pyenv install <python version number: such as 3.8.10>

       ​ ​Set the default Python version in the system: pyenv global <python version number>

   2. pyenv global does not take effect

        It may be because the path path is not configured correctly. You need to add the following content to the ~/.zshrc file:

            export PYENV_ROOT=\"$HOME/.pyenv\"

            export PATH=\"$PYENV_ROOT/bin:$PATH\"

            eval \"$(pyenv init —path)\"

        Then save and close the file, and then re-run "pyenv global <Python version number>" to set the Python version globally.

Note that if your version of Python is already installed in your system Python, you will need to remove it from your PATH to avoid possible conflicts.

3. Solve the problem of Mac terminal command failure

      Error environment variable configuration causes the terminal command to fail, prompt ZSH: Command Not Found:, add this command to save: export path = & quot;/usr/local/bin:/usr/local/sbin: ~/bin: $ path & quot;

      Method 1: Open the environment variable configuration file: open ~/.zshrc to edit directly, then command s to save;

      Method 2: vim ~/.zshrc, command line editing, enter i to enter editing, esc to exit editing, :wq to save and exit

Guess you like

Origin blog.csdn.net/sinat_37905754/article/details/133746995