0

I have python 2.7.15+ installed and I want to upgrade to the latest version 3.7. I have come across the command from net

sudo apt-get install python 3.3.3

But this command adds another software right, it's not replacing python 2.7 How should I proceed? Would be grateful for any help.

Distributor ID: Ubuntu

Description: Ubuntu 18.04.3 LTS

Release: 18.04

Codename: bionic

5
  • 1
    Ubuntu 18.04 comes with python3 (3.6.5-3) installed. python2 is a different system to python3, and python2 doesn't upgrade past 2.7.17-1 (packages.ubuntu.com/… packages.ubuntu.com/…). Check you've applied all upgrades, as your system should report 18.04.4 (not 18.04.3)
    – guiverc
    Mar 5, 2020 at 8:28
  • 1
    You cannot remove Python2 from Ubuntu so far. You can only add Python3 on your Ubuntu installation. It is expected that Ubuntu 20.04 will be "Python2-free", but I doubt it. :(
    – FedKad
    Mar 5, 2020 at 8:31
  • @FedonKadifeli Ubuntu 20.04 & flavors ISOs have been completely free of python2 (it was renamed) for some time now. New installs of all flavors have no python2, which has led to some bug reports with 3rd party packages that rely on python (*being python[2]; no longer installing cleanly))
    – guiverc
    Mar 5, 2020 at 8:36
  • @FedonKadifeli I also noticed it awhile back when my system (4+ months now 20.04) had python2 removed with normal upgrades and a script I run weekly wouldn't anymore... (script was converted to python3 and all good again..) It's been tested not just with 'new' installs.
    – guiverc
    Mar 5, 2020 at 8:42
  • After the commands $ sudo apt-get update $ sudo apt-get upgrade, I got it as 18.04.4.
    – Hrushi
    Mar 5, 2020 at 12:36

1 Answer 1

3

Why would you replace python 2.7? They both can live along on the same system. You can run python3 by calling python3. You can set your preferred version (what is executed by calling python to python3), but beware if there are some system scripts needing python2 but not explicitly specifying it you might break things.

For a script that should be executed by python3 you can write:

#!/usr/bin/env python3
...

If you need a newer python3 install or guarantee its own name space (without modifying the system configuration) you need to work with virtual environments and custom distros like conda.

3
  • I am doing an online course where python 3 is used, so thought it would be better to upgrade to avoid unnecessary troubles in the future. Anyhow thanks, I would try to set up the virtual environment
    – Hrushi
    Mar 5, 2020 at 12:39
  • 1
    @KyathariHrushikesh Just run sudo apt install python3 python3-all. If you really need the latest Python then I suggest you look into using PyENV to do a Python install of the version you want in your userspace and set that as your local user shell to execute things with. You can then execute Python scripts from that by calling python3 script.py and then provided PyEnv has set a local shim for you to use for Python calls, it'll use the version of Python you specify. Without breaking system Python libs.
    – Thomas Ward
    Mar 5, 2020 at 14:10
  • Thanks a lot, @ThomasWard that was very helpful
    – Hrushi
    Mar 5, 2020 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.