Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pip with python 2 & 3 installed? (OSX)

I am trying to get python 3 working on my OSX laptop.

I need to install requests for python 3, and it isn't working.

I think I've managed to get pip installed for both python 2.7 & python 3 however...

Whenever I use 'pip' it points to python2... I can't seem to access the pip for python 3?

like image 936
Fire Dude Avatar asked Nov 12 '18 18:11

Fire Dude


1 Answers

In all likelihood, pip3 will be installed pointing to your Python 3 installation, so your use case is probably solvable by just switching from:

$ pip install foo

to:

$ pip3 install foo  # Or pip3.7 install foo if you need to disambiguate further

That said, it can get kind of complicated when you have many different Python installs, where pip/pip3 might have been installed pointing to a Python version that doesn't correspond to the python/python3 you're using, which can be quite confusing.

If you know python & python3 are the correct executable, just use it to invoke pip on your behalf. It's fairly easy too, just check your version to be sure it's the one you expect (e.g. on my system):

$ python --version
Python 2.7.15rc1
$ python3 --version
Python 3.6.6

then use the appropriate one with -mpip, a flag to run an installed module/package via the chosen Python as the "main" executable, bypassing the need for specifically versioned pip executable entirely. So if you wanted to install foo for Python 3.6 on my machine, you'd run:

$ python3 -mpip install foo

This is especially useful on Windows, where the pip executables often either don't exist, or are not installed in the PATH, so it's irritating to use them. Instead, use the Windows launcher that comes with any modern Python 3 version (but manages all Python versions on the machine), and is used to disambiguate among various versions. For example:

C:\>; Installs foo for latest installed version of Python 3
C:\>py -3 -mpip install foo
C:\>; Installs foo for latest installed version of Python 2
C:\>py -2 -mpip install foo
C:\>; Installs foo for latest installed version of Python 3.6
C:\>py -3.6 -mpip install foo

Essentially, any use of pip can be replaced by executing the Python interpreter directly with the -mpip option to run the pip package as the "main" executable.

This trick applies to many other tools with dedicated launchers that are often not installed in the PATH, particularly on Windows, and it makes updates easier too; my Windows shortcut for launching ipython3 never used a hardcoded path to the launcher (e.g. C:\Program Files\Python36\Scripts\ipython3.exe), instead using %WINDIR%\py.exe -3 -mIPython. In addition to being more portable (the shortcut "just works" on any Windows system with a semi-recent Python 3 install), it's self-updating; when I upgraded from 3.6 to 3.7, the shortcut didn't have to change at all (I had to run py -3 -mpip install ipython again to get IPython reinstalled, but once I'd done that, the shortcut seamlessly began referring to the 3.7 install with no changes needed).

like image 85
ShadowRanger Avatar answered Sep 28 '22 03:09

ShadowRanger