64

I am using OSX and I have pip installed for both Python3.5 and Python2.7. I know I can run the command pip2 to use Python2 and when I use the command pip3 Python3.x will be used. The problem is that the default of pip is set to Python2.7 and I want it to be Python3.x.

How can I change that?

edit: No, I am not running a virtual environment yet. If it was a virtual environment I could just run Python3.x and forget all about Python2.7, unfortunately since OSX requires Python2.7 for it's use I can't do that. Hence why I'm asking this.

Thanks for the answer. I however don't want to change what running python does. Instead I would like to change the path that running pip takes. At the moment pip -V shows me pip 8.1.2 from /Library/Python/2.7/site-packages (python 2.7), but I am looking for pip 8.1.2 from /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (python 3.5) I am sure there has to be a way to do this. Any ideas?

loki l
  • 3,435
  • 2
  • 11
  • 10
  • 2
    Are you running in a virtualenv? (and if not, you should probably do that first) – spectras Aug 14 '16 at 00:53
  • I plan on running a virtualen in the near future, but would like to get this working first. – loki l Aug 14 '16 at 00:58
  • The virtualenv will override your `PATH` so the right python is invoked. You should start with the virtualenv, it is designed exactly for that purpose. – spectras Aug 14 '16 at 01:01
  • if you didn't want to use virtual environment you could just re-organize your PATH.. – Tadhg McDonald-Jensen Aug 14 '16 at 01:27
  • @TadhgMcDonald-Jensen How would I do that? SO far the only thing I can think of doing is assigning an `alias pip='pip3'`, but I think there has to be a better way. – loki l Aug 14 '16 at 01:33
  • see http://daniel.hepper.net/blog/2011/02/change-order-of-path-entries-on-mac-os-x/, seems the best way according to that is to edit `/etc/paths` – Tadhg McDonald-Jensen Aug 14 '16 at 01:41

8 Answers8

78

Run this:

pip3 install --upgrade --force pip

or even more explicit:

python3 -m pip install --upgrade --force pip

This will install pip for Python 3 and make Python 3 version of pip default.

Validate with:

pip -V
48

I always just run it via Python itself, this way:

python3 -m pip install some_module

or

python2 -m pip install some_module

The -m calls the __main__.py module of a specified package. Pip supports this.

Ghostkeeper
  • 2,830
  • 1
  • 15
  • 27
  • 8
    Thanks for the answer. I know I can do that, but that's not what I was looking for. I want `pip` to lead to Python3.x by default. – loki l Aug 14 '16 at 00:52
10

Can't you alias pip='pip3' in your ~/.bash_profile?

In Terminal, run nano ~/.bash_profile, then add a line to the end that reads alias pip='pip3'. This is safe; it won't affect system processes, only your terminal.

Luke Taylor
  • 8,631
  • 8
  • 54
  • 92
  • 2
    …but any bash script you run that has a `pip` in it will still run `/usr/bin/pip`, as aliases are not expanded in subscripts. So be very cautious with this. – spectras Aug 14 '16 at 01:21
  • Right. But if OP is looking for convenience when installing packages normally, this is the way to go. For my terminal, I have `python=ipython3`, `python2=ipython`, `vanilla-python=python3` and `vanilla-python2=python`, and `pip=pip3`, and it makes working in Python 3 much faster. – Luke Taylor Aug 14 '16 at 01:22
  • 1
    @LukeTaylor Thanks luke, I didn't want to resort to that, but It'll have to do for now until I figure how to change the actual path. Thanks – loki l Aug 14 '16 at 01:35
7

For your projects, you should be using a virtualenv.

You can choose which python will be that of the virtualenv at creation time, by specifying it on the command line:

virtualenv -p python3 env
# then
. env/bin/activate
python              # ← will run python3

That python interpreter will be the one used when you run python or pip while the virtualenv is active.

Under the hood, activating the virtualenv will:

  • modify your PATH environment setting so binaries in env/bin override those from your system.
  • modify your PYTHONHOME environment setting so python modules are loaded from env/lib.

So python, pip and any other package you install with pip will be run from the virtualenv, with the python version you chose and the package versions you installed in the virtualenv.

Other than this, running python without using virtualenv will just run the default python of the system, which you cannot usually change as it would break a lot of system scripts.

spectras
  • 13,105
  • 2
  • 31
  • 53
  • I've never used `virtualenv` besides heavy python-ing, this is one of the first compelling cases I've seen for my Python usage (though I haven't really gone looking for any) – Luke Taylor Aug 14 '16 at 01:17
  • It is a good idea for other reasons as well. For instance, it guarantees that upgrading your system will not break your project (though you might have to update the virtualenv). – spectras Aug 14 '16 at 01:27
  • ah, neat. I'll look into it – Luke Taylor Aug 14 '16 at 01:28
  • And how to make pip a pip3 command by default in a virtual environment of python2.x? Since it does not help changing the executing command when you have sub-commands in the files calling the old pip again - and you need to install it in that virtual venv. – questionto42 Dec 15 '20 at 22:49
3

It works for me:

As super-user

Uninstall pip

sudo pip uninstall pip

Install pip

sudo python3 -m pip install --upgrade --force pip

Check install path

sudo pip -V

As local-user

Uninstall pip

pip uninstall pip

Install pip

python3 -m pip install --upgrade --force pip

Check install path

pip -V
  • 1
    /usr/local/bin/python: No module named pip.__main__; 'pip' is a package and cannot be directly executed – MAZ Sep 10 '21 at 12:59
1

Although PEP 394 does not specifically mention pip, it does discuss a number of other Python-related commands (including python itself). The short version is that, for reasons of backwards compatibility, the unversioned commands should refer to Python 2.x for the immediate future on most reasonable systems.

Generally, these aliases are implemented as symbolic links, and you can just flip the symlink to point at the version you want (e.g. with ln -f -s $(which pip3) $(which pip) as root). But it may not be a good idea if you have any software that expects to interact with Python 2 (which may be more than you think since a lot of software interacts with Python).

The saner option is to set up a Virtualenv with Python 3. Then, within the Virtualenv, all Python-related commands will refer to 3.x instead of 2.x. This will not break the system, unlike the previous paragraph which could well break things.

Kevin
  • 28,963
  • 9
  • 62
  • 81
  • Thanks for the answer @Kevin, do you think that changing `pip` will break the compatibility though? I want to leave the `python` default to Python2.7 I just want to change the version that `pip` points to, it's all. – loki l Aug 14 '16 at 01:07
  • @lokil> you should not install packages with `pip` outside of a virtualenv anyway, it will break your python installation. – spectras Aug 14 '16 at 01:08
  • @spectras I will take your advice and do virtualen, but my question still stands. If I wanted to change the path that `pip` points to... How would I do it? – loki l Aug 14 '16 at 01:13
  • 1
    As Kevin says in this very answer, you would delete `/usr/bin/pip` and recreate it as a link to the one you want, using the command he put in his answer. And you'd probably be left with a non-working python after using it for some time. – spectras Aug 14 '16 at 01:19
0

Since you have specified in the comments you want syntax like pip install [package] to work, here is a solution:

  1. Install setuptools for Python3: apt-get install python3-setuptools

  2. Now pip for Python3 could be installed by: python3 -m easy_install pip

  3. Now you can use pip with the specific version of Python to install package for Python 3 by: pip-3.2 install [package]

Ganesh Kathiresan
  • 2,068
  • 2
  • 22
  • 33
EzioAuditore
  • 54
  • 1
  • 3
-1

Why not just repoint the link /bin/python to python3? It seems like the easiest solution. Especially if you want it for all users of your system.

Remedyman
  • 19
  • 3