KMC
KMC

Reputation: 20046

How does my Python point to Python3 without setting PATH?

My mac came with Python2 by default. I installed Python3 using Homebrew (brew install python3) and did something to get python to point to python3 in Terminal. Unfortunately I can't remember what I did (I must have casually copied and pasted a couple lines of commands to get python set to python3). Now that I want to find out what I did that was forgotten, I looked everywhere in my $PATHS, my .bash_profile, .zhrc etc but I found nothing set for python or python3 - no PATHs written, no aliasing either.

How is that possible and what else could I have done?

My Python versions:

my-mac:~ myname$ python2

Python 2.7.16 (default, Feb 28 2021, 12:34:25) 
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

my-mac:~ myname$ python

Python 3.9.6 (default, Jun 29 2021, 05:25:02) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Here are all my PATHs:

my-mac:~ myname$ echo "${PATH//:/$'\n'}"

/Users/myname/.rvm/gems/ruby-1.9.3-p327/bin
/Users/myname/.rvm/gems/ruby-1.9.3-p327@global/bin
/Users/myname/.rvm/rubies/ruby-1.9.3-p327/bin
/Users/myname/.rvm/bin
/usr/local/mysql/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin
/Users/myname/Qt/5.5/clang_64/bin/

Checking Python and Python3 paths:

my-mac:~ myname$ type python
python is hashed (/usr/local/bin/python)

my-mac:~ myname$ type python3
python3 is /usr/local/bin/python3

My .bash_profile and .zshrc content:

my-mac:~ myname$ nano .bash_profile

export PATH="/usr/local/mysql/bin:$PATH"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

my-mac:~ myname$ nano .zshrc

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting

Upvotes: 0

Views: 451

Answers (1)

Chen A.
Chen A.

Reputation: 11280

brew install the binaries at /usr/local/bin which is before /usr/bin in your PATH. (brew updates the PATH to include it prior to the systems /usr/bin). You don't need to do anything to put brew installed apps in your PATH, it's already there.

Try which python and see where it points. Here's an example of MacOS default interpreter.

➜ which python
/usr/bin/python
➜ python
Python 2.7.16 (default, Jun 18 2021, 03:23:53)
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D

If you want python to point to the system's Python 2.x, you can delete /usr/local/bin/python. Then to use Python3, you'll need to specify python3. (which is located at /usr/local/bin/)

You can list /usr/local/bin which contains symlinks to the actual binaries.

➜ ll /usr/local/bin | grep python
lrwxr-xr-x  1 usr  admin    38B Sep  5 12:27 python3 -> ../Cellar/[email protected]/3.9.6/bin/python3
lrwxr-xr-x  1 usr  admin    45B Sep  5 12:27 python3-config -> ../Cellar/[email protected]/3.9.6/bin/python3-config
lrwxr-xr-x  1 usr  admin    40B Sep  5 12:27 python3.9 -> ../Cellar/[email protected]/3.9.6/bin/python3.9

Upvotes: 1

Related Questions