2
votes

I have installed python3 on my raspberry 4, and i guess there is a native python2 as well. I must use a library that needs python3.6+, and it checks the version of pip (not pip3!) and if it finds out that it points to python2, it throws an exception. My pip command is stuck pointing to python2.7 and I would like it to point to python3 instead. I've tried many things from other answers:

python -m pip install --upgrade --force-reinstall pip

and

pip3 install --upgrade --force pip

then

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

None of the above changed the behaviour, and I still get this when i run pip -V: Warning: pip is being invoked by an old script wraper...[...]

pip 20.02 from /home/pi/.local/lib/python2.7/site-packages/pip (python 2.7)

What can I do? Thanks

3
You should always use the pythonX.Y -m pip form. -- snarky.ca/why-you-should-use-python-m-pipsinoroc

3 Answers

4
votes

why don't you use update-alternatives?

sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 3
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip2 2
# the syntax is like: sudo update-alternatives --install <path> <name> <alternative> <priority>
# then you might change the default pip (which must by now be pip3) like this
sudo update-alternatives --config pip

one can consider this solution as an easier/cleaner alternative to symlink

3
votes

Upgrading pip won’t solve your problem, this source of the problem is the fact that you have both python2 and python3 on your system.

Solution 1 - virtual environment

(Which I recommend)

using

python3 -m venv venv

To create a virtualenv

then activate it using

source venv/bin/activate

reinstall the required dependencies if needed, and pip should work. Read More about it here. When you'be finished working, deactivate using deactivate. Remember that you have to activate an environment every time

Solution 2 - Symlink or Copy

if you don’t want isolated environments you can symlink (Or copy over) the pip3 path to pip path so that running pip is like running pip3. Find the two paths using

which pip
which pip3

Then symlink them using

ln -s <pip3 path> <pip path>

Or copy over it using

cp <pip3 path> <pip path>

If at some point you want to reverse this (which is to keep pip and pip3 you can remove the package /home/pi/.local/lib/python2.7/site-packages/pip And install it again using get-pip.py

Solution 3 - update-alternatives

the update-alternatives command might be easier than symlinking see @erfan's answer for details

sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 3
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip2 2
sudo update-alternatives --config pip
-1
votes

Use pip3 instead of pip. If you do want to use the pip command instead of pip3, create an alias for it in your .bashrc file (or .zshrc file if you have zsh shell). This file is located in your home directory (/home/pi or ~)

Add this line to .bashrc or .zshrc:

alias pip="pip3"

Or use a python3 virtualenv. This is recommended for projects that you work on and won't want to install dependencies of that project globally. Simply go to your project folder and type:

python3 -m venv venv
source venv/bin/activate # Command to activate venv
# and now you can use pip command instead of pip3

Do remember that you have to reactivate venv everytime you open a terminal