Saturday 30 December 2023

Getting started with Python in Jupyter Notebooks

So, as we get ready to start a new year, I'm tinkering with a bunch of new ( to me ) tech toys, including wrangling Python dependencies.

Having spent much of of the past year or so using pip and pyenv to manage Python dependencies, more recently, I've been looking at an alternate solution, Anaconda.

Having downloaded and installed the Mac version of Anaconda - and allowed it to install itself into ~/.zshrc ( as I use Z Shell rather than, say, Bash ), when I start a new Terminal window ( I'm using iTerm 3.4.23 on macOS 14.2.1 ), I automatically open up in a virtual environment called base 

conda env list

# conda environments:
#
base                  *  /Users/hayd/anaconda3

type conda

conda is a shell function from /Users/hayd/.zshrc


This also gives me access to Jupyter : -

type jupyter

jupyter is /Users/hayd/anaconda3/bin/jupyter

jupyter notebook

  _   _          _      _
 | | | |_ __  __| |__ _| |_ ___
 | |_| | '_ \/ _` / _` |  _/ -_)
  \___/| .__/\__,_\__,_|\__\___|
       |_|

Read the migration plan to Notebook 7 to learn about the new features and the actions to take if you are using extensions.

https://jupyter-notebook.readthedocs.io/en/latest/migrate_to_notebook7.html

Please note that updating to Notebook 7 might break some of your extensions.

[W 15:06:56.602 NotebookApp] Loading JupyterLab as a classic notebook (v6) extension.
[I 2023-12-29 15:06:56.605 LabApp] JupyterLab extension loaded from /Users/hayd/anaconda3/lib/python3.11/site-packages/jupyterlab
[I 2023-12-29 15:06:56.605 LabApp] JupyterLab application directory is /Users/hayd/anaconda3/share/jupyter/lab
[I 15:06:57.709 NotebookApp] Serving notebooks from local directory: /Users/hayd
[I 15:06:57.709 NotebookApp] Jupyter Notebook 6.5.4 is running at:
[I 15:06:57.709 NotebookApp] http://localhost:8888/?token=707355ee0dd1f8fd9bf4e7124b83d1126a024cc4606eade0
[I 15:06:57.709 NotebookApp]  or http://127.0.0.1:8888/?token=707355ee0dd1f8fd9bf4e7124b83d1126a024cc4606eade0
[I 15:06:57.709 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 15:06:57.711 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///Users/hayd/Library/Jupyter/runtime/nbserver-33550-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=707355ee0dd1f8fd9bf4e7124b83d1126a024cc4606eade0
     or http://127.0.0.1:8888/?token=707355ee0dd1f8fd9bf4e7124b83d1126a024cc4606eade0
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.


which automatically opens up a browser tab: -


jupyter notebook list

Currently running servers:
http://localhost:8888/?token=707355ee0dd1f8fd9bf4e7124b8ad1126a024cc4606eade0 :: /Users/hayd


shows me the running notebook, plus it's port AND authentication token :-)

With the Jupyter server running, I quickly created a new Python3 ( pykernel ) notebook: -



and am off to the races: -



For reference, I'm working on watsonx.ai as a coder at present, and am running through some of the Python-based prompt engineering tutorials, and wanted a place to test the generated Python code

This was of use, in terms of getting me started: -



Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...