Deke
Deke

Reputation: 4639

How to install python 3 via mac terminal?

I did brew install python3

when i run python --version it shows 2.7

Document says: If you still see 2.7 ensure in PATH /usr/local/bin/ takes precedence over /usr/bin/

How do I go about this?

Upvotes: 1

Views: 476

Answers (1)

traeki
traeki

Reputation: 33

First, you'll want to verify the problem is as stated.

Try running e.g.

> echo $PATH
/usr/local/bin:/Users/jsh/code/go/bin:/usr/bin:/bin:/usr/local/sbin:/usr/texbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

You'll notice that the output is a series of paths separated by ':' characters. Your terminal looks in those locations, in order, until it finds something that matches your command (in this case 'python') and it runs that binary.

This means that earlier items take precedence over later items. You'll note that in my listing, /usr/local/bin/ is at the very beginning, and /usr/bin/ is somewhat later in the listing.

If your listing also has this ordering, you're getting a misleading message, and will need to dig deeper. Hopefully, however, you will find that either /usr/local/bin/ isn't in your list at all, or it is for some reason occurring after /usr/bin/. That's good -- you've now identified the problem -- and we're now at your nominal question, vis: how do you change the ordering?

The environment variable $PATH can be modified by any number of startup tools. There are probably ways to systematically observe all the things that are editing your $PATH, but what I would do is

1) Search in your home directory for things that might be messing with $PATH, something like:

> egrep -d skip -l PATH .*
.gvimrc
.zhistory
.zshrc

.zhistory, here, is a red herring -- it's a list of the stuff I've done in my shell, so I can ignore it.

.gvimrc is also misleading, because it's actually setting $GOPATH, not $PATH.

So the only configuration file I have messing with my $PATH, at least in my home directory, is .zshrc, the config file for my shell itself. This is typically true, and you can just start by assuming this, but it's always good to do a quick search...

2) Edit the responsible file until it is setting PATH so that /usr/bin/local/ precedes /usr/bin/. Open the file in your choice of editor and either modify the line that sets PATH, or add a line that prepends /usr/local/bin/ to PATH (if you go with the latter, make sure that prepend happens after all other modifications to the variable).

For example, my .zshrc has a block like this:

# PATH setup (Starting with hard reset)
export PATH=/sbin
export PATH=/usr/sbin:$PATH
export PATH=/usr/texbin:$PATH
export PATH=/usr/local/sbin:$PATH
export PATH=/bin:$PATH
export PATH=/usr/bin:$PATH
export PATH=/Users/jsh/code/go/bin:$PATH
export PATH=/usr/local/bin:$PATH

(Note that the FINAL LINE is the one prepending /usr/local/bin !)

Then restart your shell (or prompt it to reload the config file, but I'm betting if you knew how to do that, you wouldn't have needed this guidance!). Now re-run 'echo $PATH', and verify that the ordering is correct. If so, try running python again, and you should have an uncle named Bob.

Good luck!

Upvotes: 1

Related Questions