In homebrew how do I change the python3 symlink to only "python"
Asked Answered
C

5

14

I want to install python using homebrew and I noticed there are 2 different formulas for it, one for python 2.x and another for 3.x. The first symlinks "python" and the other uses "python3". so I ran brew install python3.

I really only care about using python 3 so I would like the default command to be "python" instead of having to type "python3" every time. Is there a way to do this? I tried brew switch python 3.3 but I get a "python is not found in the Cellar" error.

Connect answered 8/3, 2013 at 23:1 Comment(1)
If you really want to do this, don't. Instead use virtualenv.Island
N
8

You definitely do not want to do this! You may only care about Python 3, but many people write code that expects python to symlink to Python 2. Changing this can seriously mess your system up.

Niphablepsia answered 8/3, 2013 at 23:7 Comment(5)
If you do it in /usr/local/bin or some such, you probably won't screw up any system programs which should be explicitly using /usr/bin. But, I agree that most distributions and users still assume python means python2. That will change eventually. And you certainly could choose to be on the cutting edge for your own system. There is already at least one exception: I believe Arch Linux ships with python linked to python3.Insanitary
Of course. However, with Arch, devs are expecting Py3. If you give a program Py3 when it expects Py2, you will probably toast the program. There are exceptions, with Cross Python Compatibility (which is something I strive for), but in most cases, you are going to hit a print xxx or a x = y / 3 # int expected and end up in a train wreck.Niphablepsia
True but presumably the OP is setting up his Mac for himself (Homebrew is Mac only). User beware.Insanitary
If they want their hashbangs (or whatever) to refer to python2, just write python2. It seems somewhat irresponsible to just rely on python, unless your code will run on both. This is such a trivial thing I don't understand why it would be a problem.Chickamauga
@Chickamauga Because, way long ago, before anyone used Python 3, everyone was told to use /bin/env python. The milk has been spilled now. We can stand around and complain, or we can just live compatible. You'll note that the OP was asking a question about reducing what he typed by one character. Hey, that's what Python 2 programmers have been doing for the last however many years. Python 2 was Python.Niphablepsia
P
5

If you're doing this for personal use, don't change the symlink for python. Many of your system programs depend on python pointing to Python 2.6, and you'll break them if you change the symlink.

Instead, pick a shorter name like py and write an alias for it in ~/.bashrc, like alias py=python3.

For example, with testing:

$ echo "alias py=python3" >> ~/.bashrc
$ bash
$ py
>>> 3+3
6

This will give you the convenience without effecting the system or other users.

Phrensy answered 5/8, 2015 at 17:6 Comment(0)
H
3

If you are absolutely sure that you will never want to install / use Python 2, I think you can just create additional symlinks in /usr/local/bin. Check for everything that links to something in

../Cellar/python3/3.3.0/

and create a link without the 3 at the end, like

python -> ../Cellar/python3/3.3.0/bin/python3

Think twice though, why give up the advantages of having two Pythons side-by-side? Maybe just use the homebrew Python as intended, and create your Python 3 environments with virtualenv.

Housewife answered 19/3, 2013 at 10:22 Comment(0)
C
1

Yes, far better to use [virtual environments] (https://docs.python.org/3/library/venv.html) for python 3 than mess with the system default

pyvenv /path/to/new/virtual/environment

which will setup python 3 as the default python and also isolate pip installs to that environment which is what you want to do on any project.

Callender answered 23/11, 2016 at 13:42 Comment(0)
G
1

As mentioned this is not the best idea. However, the simplest thing to do when necessary is run python3 in terminal. If you need to run something for python3 then run python3

Grandmotherly answered 12/3, 2017 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.