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.

BioXD
  • 357
  • 1
  • 2
  • 7

5 Answers5

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.

pydsigner
  • 2,779
  • 1
  • 20
  • 33
  • 2
    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`. – Ned Deily Mar 09 '13 at 00:12
  • 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. – pydsigner Mar 09 '13 at 00:44
  • True but presumably the OP is setting up his Mac for himself (Homebrew is Mac only). User beware. – Ned Deily Mar 09 '13 at 00:51
  • 1
    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. – Kevin Jun 06 '15 at 21:14
  • @Kevin 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. – pydsigner Jun 08 '15 at 14:26
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.

Merchako
  • 789
  • 1
  • 8
  • 19
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.

lutzh
  • 4,917
  • 1
  • 18
  • 21
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.

CpILL
  • 6,169
  • 5
  • 38
  • 37
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

mrabins
  • 197
  • 1
  • 2
  • 10