which python
which 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 Answers 4


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.

  • 5
    There is no reason why you could not have python point to python3 instead of python2. Ubuntu even provides a package that implements a symbolic link for python to version 2 or 3. Of course indeed, Python 2 scripts break on python 3.
    – vanadium
    Commented Jun 9, 2022 at 16:48
  • 3
    @vanadium There is nothing in the operating system stopping you from linking anything to anything else you like, correct. That has nothing to do with my answer, which is not about can you do this but should you do this.
    – cjs
    Commented Jun 9, 2022 at 16:51
  • There was a time (pre version 3) when python was just that … regardless of the version number and no one thought about it twice … then came python3 the first ever intentionally backwards incompatible Python release and now people argue whether python is python2 or python3 … I wonder what will python4 bring with it :)
    – Raffa
    Commented Jun 9, 2022 at 17:26
  • 1
    The upstream Python Project answer: PEP 394
    – user535733
    Commented Jun 9, 2022 at 18:14
  • 1
    @vanadium True, as long as the user understands the consequences and is willing to be responsible for fixing any breakage. However, users seeking an easy answer without willingness to encounter consequences would be wise to heed this answer. Both answers are good.
    – user535733
    Commented Jun 9, 2022 at 18:36

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.

  • I renamed the python executable and created a symbolic link to python3 named python then checked versions for both python and python3 but the versions were different. Then I removed the symbolic link and copied python3 executable and named python still the versions are different Commented Jun 9, 2022 at 8:35
  • Then you are copying/linking different executables than these that show up with which, or something else is going on we cannot see from here.
    – vanadium
    Commented Jun 9, 2022 at 15:55

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.

  • This is the first anwer pointing to the correct approach for this specific question, i.e. using the pyenv tools to configure which python version you want to run through the command python. Please explain a bit about shim and provide more concise instructions on how to have python run python3. The alias option very likely has little relevance - I suggest to delete that.
    – vanadium
    Commented Jun 10, 2022 at 8:15

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.

  • The shebang -only- plays a role if the script is executable -and- run without explicitly calling the interpreter in the command. If the script is not executable, the command will fail, despite your shebang, when calling the interpreter in the command, you might as well have "#!/monkey/eats/banana" as a shebang, it'll still work, from the interpreter that is called in the command (if the script is valid for that python version). Commented Jun 10, 2022 at 6:27
  • Question indeed here is to execute python, and have that be python3.
    – vanadium
    Commented Jun 10, 2022 at 8:08
  • Right, I should have mentioned this only applies when you treat the script as executable. The @cjs answer is more detailed and better than mine; as to whether or not they're on-topic for the OP the question seems likely to come up as a hit when people are searching for ways to run the right version of python from a script, so hopefully the content will help someone. Commented Jul 5, 2022 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .