5
which python
/home/tanvir/.pyenv/shims/python
which python3
/home/tanvir/.pyenv/shims/python3

I want python to point /home/tanvir/.pyenv/shims/python3 so that once I run python path/to/script it runs python3 path/to/script

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:    20.04
Codename:   focal

4 Answers4

8

I suggest you do not do this. Pythons 2 and 3 have some incompatibilities and for a long time the convention was for hashbangs at the top of Python scripts to select 2 with #!/usr/bin/env python and 3 with #!/usr/bin/env python3. The former convention is starting to vanish, but you may still come across old scripts that are specifically trying to select Python 2 with #!/usr/bin/env python and your configuration of having python run Python 3 will break them.

cjs
  • 344
6

Although you did not explicitly tell, you have set up and are using pyenv. This is a tool written in bash that allows to manage different python version on a per user basis.

To change your default python version, you need to use the tool. Currently, your python version may still be set to the python version installed with the Ubuntu system.

  1. List the available python versions with the command
    pyenv versions
    
  2. Then change the default python version with a command like
    pyenv global 3.6.8
    
    Adapt 3.6.8 according to the output you found in step one.

Besides setting the global default python version for you user, you can set different default python version for different projects. Inform yourself to learn more about the tool pyenv.

  • Beware that which python will continue to point to /home/tanvir/.pyenv/shims/python. This is because of the way pyenv works. That executable is a "shims", a short bash script that converts your python command to a call to the configured default python version.

  • If step 1 does not reveal any python version apart from system, you will need to install some python versions using pyenv.

For users not using pyenv, Ubuntu provides packages to install a symbolic link that points to the desired python version, i.e., python-is-python2 and python-is-python3. On the latest Ubuntu versions, python2 is not installed by default, and only the latter package is available.

vanadium
  • 97,564
4

It looks as though you are using pyenv which is designed for the purpose of selecting which python version to use. The documentation on that page has instructions for changing your selection. One way is to run : $ pyenv global 3.9.0 (change as appropriate for the python versions you have installed. pyenv versions will show you)

Neither /home/tanvir/.pyenv/shims/python nor /home/tanvir/.pyenv/shims/python3 are real python executables, but are shims that pass through to one of the installed python versions depending on the pyenv setting.

It's not clear what context you want to use python3 as a default in:

  • Globally for you as a user
  • In a single shell session
  • Globally for all users
  • For a single project

There are different ways and tools for each of these, e.g. if it's a single project, then virtualenv (pyenv-virtualenv) may be the best tool. For a single shell session running $ alias python=python3 may be the best option.

rhellen
  • 394
1

There's another approach that is starting to gain some traction, which is to use #!/usr/bin/env python3 (or equivalent with python2). By using this variation, it will pick up whichever matching version of python is in your path first, but this allows you to have different virtual environments without hard-coding the path to the system-installed python.

Then you can just run path/to/script and it will load the expected version of python for you without having to specify it on the command line.