Explain Codes LogoExplain Codes Logo

How to know which Python is running in Jupyter notebook?

python
prompt-engineering
venv
best-practices
Anton ShumikhinbyAnton Shumikhin·Feb 19, 2025
TLDR

Grab your Python version in the Jupyter notebook slickly with this one-liner:

!python --version

Employing !python --version, this command executes a shell command to display your Python version swiftly. It's a quick way to glimpse into your running Python version details.

Direct probe to Python interpreter path

To obtain a bird’s eye view, you can examine the path your Python interpreter has latched onto, by:

import sys print("Python Interpreter Path:", sys.executable) # Return to base, here's the path!

With this, you will access the holistic path of your Python executable, manifesting clarity about the Python interpreter being adopted by Jupyter, super handy if your system houses multiple Python installations.

Journey through kernel environments

Altering your Python environment in Jupyter is achievable through the menu: KERNEL >> CHANGE KERNEL. Each kernel is linked to a distinct Python environment. Can't spot your required environment? Time for some environment creation and kernel addition!

Steering environments with Conda

Setting up a new environment specifying a Python version is conveniently possible with the conda create command:

conda create -n myenv python=3.8 # Just like custom ordering your favourite pizza.

Activation of your environment comes next, followed by the installation of Jupyter and the necessary packages within this environment:

conda activate myenv pip install notebook

The final step lets you link your new environment to Jupyter:

python -m ipykernel install --user --name=myenv # Now you're talking!

Configuration and customization finer points

Integration of Anaconda to your system PATH is paramount for a smooth juggling of tasks. conda init offers an automated configuration, steering clear from manual edits which often lead to errors. Before fingering PATH variables, talk to your mentor Anaconda's documentation for correct settings adherence.

You can tailor your Jupyter kernel for more coherent namespace management using additional ipykernel installation arguments such as --user or --name.

Surveying available kernels

For sketching out the available kernels accommodated by Jupyter, the command comes in handy:

!jupyter kernelspec list # Jupyter's most-wanted list!

This list narrates the stories of all environments acknowledged by Jupyter, a vital asset for verification of the correctness of your setup.

Deep-diving into documentation: your journey guide

For an enhanced clarity and confidence in dealing with Python environments in Jupyter, there lies a treasure-trove of official documentation. In that ocean of information, you can find gems to mastery—managing environments, configuring paths, and more.

You'll also grasp more efficient ways of checking your Python version by flicking through the pages of Python's platform library. This library paves the road for precision and depth of your interpreter and OS insight.

For the adventurers seeking greater flexibility and control, diverse IPython profiles may be your magic portal. IPython profiles are your Swiss army knife that allows for a more exciting and personalized environment management.