Home > Backend Development > Python Tutorial > How Can I Make Python 3 the Default Python Version on macOS?

How Can I Make Python 3 the Default Python Version on macOS?

DDD
Release: 2024-11-23 08:33:21
Original
361 people have browsed it

How Can I Make Python 3 the Default Python Version on macOS?

Setting Python 3.x as the Default on macOS

To set Python 3.x as the default on macOS, you can create an alias in your shell.

Steps:

  1. Open your command shell (Terminal, iTerm2, etc.).
  2. Add the following line to your .profile file, which is located in your home directory (~/.profile):
alias python='python3'
Copy after login
  1. Source your .profile file in your shell:
  • For bash (macOS 10.14 and below):

    [ -e ~/.profile ] && . ~/.profile
    Copy after login
    Copy after login
  • For zsh (macOS 10.15 and later):

    [ -e ~/.profile ] && . ~/.profile
    Copy after login
    Copy after login

Note: Changing the default Python executable system-wide may break applications that depend on Python 2. To avoid this, you can create an alias instead of modifying the default executable.

Additional Tips:

  • To invoke the original Python 2 interpreter, use the python2 command.
  • For scripts, use #!/usr/bin/env python3 as the shebang line to explicitly use Python 3 instead of the default system interpreter.

The above is the detailed content of How Can I Make Python 3 the Default Python Version on macOS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template