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
  • 324
  • 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 Jun 09 '22 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 Jun 09 '22 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 Jun 09 '22 at 17:26
  • 1
    The upstream Python Project answer: PEP 394 – user535733 Jun 09 '22 at 18:14
  • @user535733 in your link I see what I wrote above: python can be python2 or python3. Or do I miss something? User wants to make python = python3. This question is not related to the shebang, where the recommendation is to be specific. – vanadium Jun 09 '22 at 18:21
  • @cjs To me, there is nothing in the current question that is not in agreement with the recommendation of the Python Project, linked to by user535733 in the comment above. – vanadium Jun 09 '22 at 18:24
  • 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 Jun 09 '22 at 18:36
  • @vanadium PEP 394's suggestion that "...publishers can expect users of the software to provide a suitable execution environment" seems faintly ridiculous to me. Does the OP appear to you to be qualified to do this? – cjs Jun 09 '22 at 18:52
  • I am not judging the skills of the OP. The question is fully conforming to the recommendation of PEP 394, and thus deserves, in my opinion, an objective answer on how to have python point to python3. – vanadium Jun 09 '22 at 20:22
  • @vanadium I am glad you agree with me that the question deserves an objective answer on how to have python point to python3. I am not clear on what the point is of your comments on this answer, though. Do you think that this answer is somehow preventing other answers from being posted that explain how to change the link? – cjs Jun 10 '22 at 02:41
  • My objections 1) it is fully acceptable to have python point to python 3, so there is no objective reason to discourage this - it even goes against the PEP recommendations. Of course, indicate possible caveats along with an answer is fine. I also do not think your information with respect to the shebang is correct. – vanadium Jun 10 '22 at 08:06
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
  • 88,010
  • 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 – Tanvir Ahmed Jun 09 '22 at 08: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 Jun 09 '22 at 15:55
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
  • 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 Jun 10 '22 at 08:15
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.

  • 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). – Jacob Vlijm Jun 10 '22 at 06:27
  • Question indeed here is to execute python, and have that be python3. – vanadium Jun 10 '22 at 08: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. – Greg Nelson Jul 05 '22 at 20:44