Simon Lindgren
Simon Lindgren

Reputation: 2041

Editors not honoring Python shell aliases

I run macOS Catalina with zshell.

Out of the box the os has one python2 and one python3 version in /usr/bin/python and /usr/bin/python3. I have installed a newer python3 via Homebrew. That version is in /usr/local/opt/[email protected]/bin/python3.

I have added aliases to my ~/.zshrc-file so that both python and python3 will launch into the 3.8 Homebrew version.

When using editors (e.g. Atom) that run python scripts by calling python3 this aliasing does not seem to work. I guess this is because it is specific to the terminal shell.

What is a better way of getting my homebrew python3.8 to become the default python on my system?

Upvotes: 0

Views: 57

Answers (1)

chepner
chepner

Reputation: 532003

Don't uses aliases for selecting alternate programs. Use your PATH variable to manage your preferences.

Start by creating a local bin directory if you don't already have one.

mkdir -p ~/bin

Assuming your PATH is already set up to prefer Homebrew versions over system-installed versions, add ~/bin to the front of the path.

# In .bash_profile
PATH=~/bin:$PATH

Now, create a symbolic link ~/bin/python to the desired Python 3 interpreter.

ln -s /usr/local/opt/[email protected]/bin/python3 ~/bin/python

Now when you run python, you'll get your Homebrew python3.8 interpreter. You can still access the system Python 2 with /usr/bin/python when needed. Your editors should also inherit and respect your PATH variable, unless it is configured to use a specific hard-coded path.

Note that Homebrew still(?) links /usr/local/bin/python to its own Python 2 interpreter; I don't recommend changing that to python3, lest other Homebrew-managed programs get Python 3 when they require Python 2, hence the use of ~/bin. (There's still a chance that programs using python via path lookup will assume it is Python 2, but this should minimize the problems.)

Upvotes: 2

Related Questions