How to know which Python is running in Jupyter notebook?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Understanding Which Python Version is Running in a Jupyter Notebook
Jupyter notebooks are a valuable tool in the data science workflow for prototyping, data visualization, and interactive computing in Python. However, situations may arise where you need to confirm which version of Python your Jupyter notebook is executing. This information is crucial, especially when you are coordinating multiple environments or resolving compatibility issues with libraries.
How to Check the Python Version in Jupyter Notebook
There are several methods to determine the version of Python running inside your Jupyter notebook. Each approach provides valuable insights and can be leveraged based on user preference or the specific context of troubleshooting.
Method 1: Using the `sys` Module
One of the most straightforward approaches is utilizing Python's built-in `sys` module. This module allows you to access some variables used or maintained by the interpreter and interact with the interpreter's environment.
- `sys.version`: A string providing a regular formatted text with detailed Python version information. This includes the version number, the build number, and the compiler used.
- `sys.version_info`: A more convenient alternative for programmatic version checking. It offers a named tuple with five components: `major`, `minor`, `micro`, `release level`, and `serial`.
- `platform.python_version()`: Returns the Python version as a string in the format 'major.minor.micro'.
- `!python --version`: By setting this as a shell command prefix (`!`), IPython identifies and runs the system-installed Python or the one captured by the `$PATH` environment variable.
- Virtual Environments: Be mindful of which virtual environment is active, as this affects the Python version. Commands like `conda info` or `pip list` can also help verify the environment details.
- Kernel Specification: Jupyter notebook allows you to choose different kernels, which can point to different Python versions. Make sure your selected kernel matches the environment you intend to use.
- Multiple Interpreters: If you have multiple Python installations, you may need to ensure that Jupyter is running the interpreter you expect. Check configuration files or use commands like `which python` (on Unix-like systems) or `where python` (on Windows) to surface path details.
Related reading
- How to label transitive groups with SQL?
- How to load a tsv file into a Pandas DataFrame?
- How to load/edit/run/save text files .py into an IPython notebook cell?
- How to locally view tensorboard of remote server
- How to know/change current directory in Python shell?
- How to leave/exit/deactivate a Python virtualenv
- How to loop over grouped Pandas dataframe?
- How to make a force directed layout with no node-edge overlapping
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.