Title: Cannot upgrade sklearn, pip pointing to python3
Tags: python;python-2.7;pip
Question: I wanted to upgrade sklearn in python 2.7 with pip but I couldn't because since a day or two pip seems to be pointing to python 3.4, not python 2.7: 

```&gt; pip install -U scikit-learn
Requirement already up-to-date: scikit-learn in /home/kinkyboy/.local/lib/python3.4/site-packages
Cleaning up...
```

This shows my current pip* commands:

```&gt; pip -V
pip 1.5.4 from /usr/lib/python3/dist-packages (python 3.4)
&gt; pip2 -V
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)
&gt; pip3 -V
pip 1.5.4 from /usr/lib/python3/dist-packages (python 3.4)
```

and this shows that python is using python 2.7: 

```&gt; which python
python is /usr/bin/python
python is /home/kinkyboy/conda/bin/python
&gt; ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Jan  6  2016 /usr/bin/python -&gt; python2.7*
```

I managed to upgrade sklearn using pip2, but how to point pip back to python 2.7? 

Update:
I tried the following and I get a permission denied error. 

```&gt; python -m pip install -U --force-reinstall pip
Collecting pip
  Using cached pip-9.0.1-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py", line 778, in install
    requirement.uninstall(auto_confirm=True)
  File "/usr/local/lib/python2.7/dist-packages/pip/req/req_install.py", line 754, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/usr/local/lib/python2.7/dist-packages/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/usr/local/lib/python2.7/dist-packages/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/usr/lib/python2.7/shutil.py", line 303, in move
    os.unlink(src)
OSError: [Errno 13] Permission denied: '/usr/bin/pip'
```

Even if I know using sudo is bad (though I might have used it in the past), I tried to run the same command with sudo (it worked), i tried to give '/usr/bin/pip' 777 permission (but it still did not help to run the normal uninstall above), and I also did a sudo uninstall python-pip (did not help the normal uninstall above). After these attempts I put everything back to what it was.

Comment: I think that pip will point to the version which was installed last. So if you want to use pip as pip2, just re-install pip2

Comment: linux mint, I forgot to write that

Comment: Thank you, I will try it this evening

Comment: Which OS are you using?

Comment: I've answered your question below, see if my solution helps you.

Here is the accepted answer: Not sure how you changed it, but you can change it back if you modify the first line of the pip file in /usr/bin/ (edit: and any other pip you have installed).  The below assumes you are modifying /usr/bin/pip:

1.) Ensure you have write access to /usr/bin/pip, either as root user OR change the permission with ```sudo chmod -c 757 /usr/bin/pip``` for the moment.

2.) Update the first line of pip from ```#!/usr/bin/python3``` (what I'm assuming is showing) to ```#!/usr/bin/python```

3.) Save ```pip``` and revert the permission back to 755 if you changed it (so nobody else messes with it).

My output before/after change:

```$ pip -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5)
$ pip -V
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
```

Without knowing exactly how it was changed in the first place, this would be my way of fixing it.  Otherwise, my primary method would be figuring out what triggered it first and try to reverse it.

Comment for this answer: I just read your answer but left more confused.  You ended up changing the pip (which you have several installed) but surprisingly when you change it to python3 it starts pointing to python 2.7? I know that's what you wanted but python3 should point to python 3.x installations?  Regardless I have updated my answer in case there are multiple installations.
Comment for this answer: The first line of the /usr/bin/pip file was already #!/usr/bin/python and the permission of the file was 755, so unfortunately this did not help
Comment for this answer: I solved the issue, see my own answer. if you edit your answer to include my solution I will gladly accept it
Comment for this answer: sorry my mistake, I changed it to python (2.7)! I corrected my answer

Here is another answer: In my case, when I installed sklearn, it was by default pointing to python3.5 where I wanted to use it with python 2.7. Following suggestion by Vitor, I ran the following command: 

```    sudo python -m pip install -U scikit-learn
```

and it worked for me!


Here is another answer: I've found this GitHub thread on Homebrew's repository with a similar problem: https://github.com/Homebrew/legacy-homebrew/issues/50607

Apparently, executing the following commands should solve your problem: 

```python3 -m pip install -U --force-reinstall pip
python -m pip install -U --force-reinstall pip
```

But you can always execute pip by using: 

```python -m pip
python3 -m pip
```

Comment for this answer: Unfortunately your suggestion did not help, I updated my question. Is it possible that I run some sudo pip install at some point and that messed things up?
Comment for this answer: Yes, if you are getting a permission denied you probably installed either pip, python or (MAYBE) some extension using sudo. Check your `/usr/bin/pip` permissions to confirm that. You can remove using `sudo` and reinstall it. I'm really not sure what will be the consequences thought. But as I said, you can always use `python -m pip` and `python3 -m pip`.
Comment for this answer: Also, take a look at this: https://github.com/Homebrew/legacy-homebrew/issues/25752

Here is another answer: I got inspired by  @Idlehands 's initial answer to check if the executable for python began with #!/usr/bin/python3 . It didn't. However, pip's executable did: 

```&gt; which pip
   pip is /usr/local/bin/pip
   pip is /usr/bin/pip
   pip is /home/kinkyboy/conda/bin/pip
&gt; diff /usr/local/bin/pip /usr/bin/pip3
   < #!/usr/bin/python3
```

I changed #!/usr/bin/python3 to #!/usr/bin/python, and after that pip -V was pointing to python 2.7. 

sklearn was the latest and I could import a module that is present in latest (that was was I wanted to do in the first place). About this: that sklearn was latest might have happened because I previously reinstalled python-pip in linux and uninstalled pip with sudo, etc, so I am not entirely positive without all this poking around sklearn would have been up to date


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:01/10/18', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<python><python-2.7><pip>', 'Title': 'Cannot upgrade sklearn, pip pointing to python3', 'ViewCount': 1448, 'id': 48180251, 'user_AboutMe': '<p>Physicist passionate about programming</p>\n', 'user_AccountId': 3613575.0, 'user_DisplayName': 'aless80', 'user_Reputation': 2852.0, 'user_Views': 353.0, 'user_id': 3592827.0}}