Search code examples
pythonjupyter-notebookgoogle-colaboratoryminiconda

Actual Python version in conda environment when running in Google Colab?


I run notebooks in Google Colab and there I install miniconda and create an appropriate environment for the application. It is good to be able to read out what Python version you actually have in the environment.

For example:

Calling platform.python_version() gives 3.10.12

Calling !Python --version gives 3.12.2

Calling !conda list python gives also 3.12.2

Seems to me that platform.python_version() refers to some other Python installation that I am not aware of. Likely in this example what !conda list gives is the relevant information for the local environment, right?

And what is this other Python installation really?


Solution

  • So, notebooks are run using python kernels. And the kernel could be different from the default kernel in your bash/zsh or CommandPrompt terminal.

    When you run this snippet you get python version that is used inside the Jupyter Notebook (Notebook kernel).

    platform.python_version()
    

    In comparison when you run the code below you get python env that is used in your bash/zsh/CommandPrompt terminal.

    !Python --version gives 3.12.2
    !conda list python gives also 3.12.2
    

    Most likely this means that your 3.12.2 environment doesn't have a jupyter lab installed and thus command jupyter points to some other environment.

    Also as a solution I would recommend to create new python kernel in the Jupyter Notebook and in the kernel path specify python path that you'd like to use during coding.