[Python] Change Python Version in Google Colab

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am confused by different outputs when I try to check the Python version used in Google Colab. In my project I need a specific Python version. Therefore, I want to downgrade Python to a specific version. I do this with this code:

    !apt-get update -y
    !update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
    !update-alternatives --config python3
    !apt install python3-pip
    !apt install python3.7-distutils


    I then check the version in four different ways:

    !python --version
    import sys
    print("User Current Version:-", sys.version)
    import platform
    print(platform.python_version())
    import os
    os.system("python --version")


    and get this output:

    Python 3.7.17
    User Current Version:- 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
    3.10.12
    0


    Based on my understanding the sys.version method should give back the version that is really used. Strangely, it is possible for me to install package versions that actually are just possible to install under Python 3.7.17. What Python version am I actually using, and what do the four different methods actually return?

    Continue reading...

Compartilhe esta Página