Python binary path in pip (linux)

Viewed 207

When I run python it runs version 3.10.4:

$ python
Python 3.10.4 (main, Apr  8 2022, 17:35:13) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[12]+  Stopped                 python

When I run python3 it runs version 3.8.10:

$ python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[13]+  Stopped                 python3

pip is using python3:

$ head -n1 /usr//bin/pip
#!/usr/bin/python3

How can I change the python path in pip to use python as below?

 #!/usr/bin/python
3 Answers

alias is a solution. Another solution is using -m to run library module.

For example, if you want to run the pip in python, you can:

python -m pip <command> [options]

run the pip in python3, you can:

python3 -m pip  <command> [options]

On my laptop:

$ python3 -m pip -V
pip 21.2.4 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

$ python -m pip -V
pip 19.3.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)

The short answer is that you don't change pip.

Ideally, you have an explicit pip binary alias for each version of python you have on your machine. I regularly use 3.7, 3.8, and 3.8, so I developed a quick script to help me switch easily. You can find it on my GitHub snek

After downloading and adding it to your path (or installing the whole lib if you like), you can just issue a simple command that will switch your python and pip commands to use a specific version of python.

snek 3.10

It only works with already installed versions though.

You can define an alias in your .bashrc called python and refer it to wherever you want.

Add this line inside your ~/.bashrc file.

alias python=/usr/bin/python3
Related