2

I used to have python and python3 installed. So I could decide which python to use. I now updated python with homebrew and it migrated python3 to python. How do I run python2 now?

When I check the installation I get:

$ python -V
Python 3.6.4
$ python2 -V
-bash: python2: command not found
$ python3 -V
Python 3.6.4

If I try to simply reinstall python2 I get:

$ brew install python2
Warning: python@2 2.7.14_1 is already installed

However I cannot use python2.

python2: command not found
jkortner
  • 549
  • 2
  • 8
  • 23
  • 1
    Use the old, out-of-date, Apple-supplied one with `/usr/bin/python` – Mark Setchell Mar 03 '18 at 11:02
  • @MarkSetchell If I use python I runs python 3. It seems that homebrew has overwritten the command. – jkortner Mar 03 '18 at 11:03
  • 1
    I didn't say to use `python`. I said to use `/usr/bin/python -V` – Mark Setchell Mar 03 '18 at 11:04
  • @MarkSetchell that works, thanks! However, how do I install packages now? I used to do everything with pip, but pip is now for python 3. `/usr/bin/pip -V` does not work. – jkortner Mar 03 '18 at 11:11
  • 1
    You can execute `pip` as a module from the Python you wish to affect... `/usr/bin/python -m pip install --user somePackage` – Mark Setchell Mar 03 '18 at 11:25
  • 1
    @MarkSetchell there seems to be no pip with the applied supplied python: `/usr/bin/python: No module named pip` – jkortner Mar 03 '18 at 11:27
  • 1
    @MarkSetchell is there any way to have 'python' point to `/usr/bin/python` python3 point to brew's python3, as before? – Deem Mar 04 '18 at 20:24

2 Answers2

3

You're missing the symbolic link that Homebrew makes from the Cellar to the actual bin directory on your path.

Use:

brew link python2

to fix that.

You may run into a warning:

Warning: python@2 is keg-only and must be linked with --force Note that doing so can interfere with building software.

See the accepted answer to this SO question for some details on that.

In most case, you can then safely use

brew link --force python2

if you're not planning building your own libraries that require the source code (libpython.so and Python.h) for Python 2.
If you do require the source code, you'll need to provide the include paths and library paths to e.g. /usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/include/python2.7 and /usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/. But that's a different topic or question.


To use pip for Homebrew's Python 2, best to use is

python2 -m pip <command>

Then you can clearly see what Python your pip command goes with, and keep it apart from Python 3 (which would be python3 -m pip).


Note

If you have Homebrew problems, first cause of action is to run

brew doctor

The error messages are usually quite helpful to fix at some of the problems.

  • 1
    Using `brew link python2` gives `Warning: python@2 is keg-only and must be linked with --force Note that doing so can interfere with building software.` – Ortomala Lokni Mar 04 '18 at 04:13
  • @OrtomalaLokni that's a related but different issue, and only relevant if you run into actual problems. An explanation is at https://stackoverflow.com/questions/17015285/understand-homebrew-and-keg-only-dependencies –  Mar 04 '18 at 05:51
  • Ok, but as `python@2` alias `python2` is now keg-only I wonder how `brew link python2` could ever work? – Ortomala Lokni Mar 04 '18 at 08:27
  • 1
    And using `brew link --force` results in warnings in `brew doctor`. – Ortomala Lokni Mar 04 '18 at 14:03
2

It seems that Homebrew has changed things again - as I had both python 2.7.x and 3.x.x installed through Homebrew for the same reasons. After the initial update, python2 would no longer work, but if you used python@2 you would get access as before.

Now however, they have seemed to partially revert some of these changes. Calling python now points to the keg-only 2.7.x homebrew installation instead of the 3.x.x installation. In addition, python@2 no longer works, but python2 no does. python3 still points to the 3.x.x installation as before.

[NOTE: I have not modified my ~/.bash_profile for any of these changes to occur.]

mtsolmn
  • 81
  • 1
  • 5