How to install `python3` without making the `python` symlink?

(Augie Fackler) #1 says that it’ll leave bare python pointing to the system Python, but after the recent updates it points to python3 from homebrew. Is there a supported way to get python3 via homebrew but NOT hijack the python command name?

(The docs also say python2 will get linked to be homebrew’s, but that appears to not match the behavior of the python2 package. I assume the docs just got overlooked on that?)

(Mike McQuaid) #2

The docs are being updated in

It is not possible to install Python (3) from Homebrew without the python command name.

(Augie Fackler) #3

Is there any way I can lobby for a reconsideration of that? One of the selling points of homebrew (long ago) was historically that it wouldn’t clobber system versions of things with incompatible versions.

(Mike McQuaid) #4

No, sorry. If you brew install python you can expect to have python in your PATH.

(Adrian Bool) #5

You’re going against the Python Language standard for this . From PEP394 - “The more general python command should be installed whenever any version of Python 2 is installed and should invoke the same version of Python as the python2 command”. As python2 is installed as part of macOS; “python” should refer to python2.

I love python3 and hardly ever use python2 - but that doesn’t mean an optional package such as python3 should be hijacking a command - for which in effect a different language - that is installed as a system component.

Should someone not want to need to type python3 to use that interpreter; it should be left it to them to decide to symlink python to python3 - but doing this as part of a package manager is not right.

(Mike McQuaid) #6

We are aware of PEP394, thanks. Arch Linux also follows this approach. The latest major version of Python is Python 3 and we are now following our own versioning scheme consistently.

(Sean Zhu) #7

@durin42 I ran into this issue today as well. What you’re looking for (for both of your questions) is to control whether the formulae are linked. What you probably want to do is this:

  • brew unlink python3
    This removes all the links that python3 installs. If you still want to run python3 easily, you might want to make the following symlinks:

    ln -s ../Cellar/python/3.6.4_3/bin/python3 /usr/local/bin/python3
    ln -s ../Cellar/python/3.6.4_3/bin/pip3 /usr/local/bin/pip3

    (Note that if you’re reading this in the future, the exact version of Python 3 you have installed might be different, so you’d have to modify that part of the command.)

    Here’s a more robust way to do this actually:

    brew install python
    brew link python  # In case you unlinked it earlier
    # Move the links you want to keep so the that the unlinker can't find them
    mv /usr/local/bin/python3 /usr/local/bin/python3.bak
    mv /usr/local/bin/pip3 /usr/local/bin/pip3.bak
    brew unlink python
    # Now move them back
    mv /usr/local/bin/python3.bak /usr/local/bin/python3
    mv /usr/local/bin/pip3.bak /usr/local/bin/pip3
  • brew link python2 --force
    The docs might have forgotten to mention that python2 does not get linked by default anymore. Run the above brew link command to install the python2 link. Note that this will not install any python links because of the recent Homebrew change; you have to call it using python2.

    If you want python to point to Python 2, you can do this:

    brew install python2
    brew link python2 --force
    cp -a /usr/local/bin/python2 /usr/local/bin/python
    cp -a /usr/local/bin/pip2 /usr/local/bin/pip

Hope this helped! I’ll definitely be re-reading this comment the next few times I need to reinstall or upgrade my Homebrew pythons. :slight_smile:

(Augie Fackler) #8

@szhu I was hoping for a supported way to tell Homebrew to Never Ever create a python that was Python 3. I’ve worked around it manually, but it’s frustrating.

(Sean Zhu) #9

I see. I think the best workaround you have right now relies on fact the Homebrew won’t install symlinks if they already exist and point to something else. So you can install and link Python 2 per my instructions above, or simply place an empty non-executable file in the locations you don’t want brew install python to overwrite, as follows:

rm /usr/local/bin/python
rm /usr/local/bin/pip
# Non-executable files are ignored when your shell locates the command to run
touch /usr/local/bin/python
touch /usr/local/bin/pip

As long as there are existing files in those locations, brew install python won’t put anything new in their places. (You’ll get an error from brew link, but it’ll actually install exactly as you want, even installing the python3 and pip3 links.)

(Mike McQuaid) #10

There is no supported way to do that.

(Inada Naoki) #11

Other workaround: put python->/usr/bin/python symlink in PATH before /usr/local/bin.

In my case, I have $HOME/local/bin and it’s before than /usr/local/bin.

$ echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
$ mkdir -p ~/local/bin
$ cd ~/local/bin
$ ln -s /usr/bin/python
$ python -V
Python 2.7.10

Since this workaround doesn’t touch /usr/local, it’s safe for brew upgrade.

(Dana Johnson) #12

This was very helpful, thank you!

(Mike McQuaid) #13

Note: we are changing the behaviour of Python 2 and 3 tomorrow: Python and PEP 394