Running Python on the Yens
In this section, you’ll check which Python version you’re using and run Python three ways: in the interactive interpreter, in a JupyterHub notebook, and as a script. You’ll also learn how $PATH and modules control which Python runs on the Yens.
Exercise: Find Your Python
In this section: Find Python with
$PATH, run code in JupyterHub, and use Claude Code to edit a plot.
Review
Start in your laptop’s terminal and connect to the Yens over SSH. Use this SSH session for the Review and Step 1; you’ll switch to JupyterHub in Step 2.
ssh SUNetID@yen.stanford.edu
Once connected:
hostname # which Yen did you land on?
ls # do you see the repo you cloned?
module avail # Remember these?
You should see your home directory and the yens-onboarding-2026 folder you cloned earlier this morning.
Step 1: Who and Where Are You?
Get your bearings first:
whoami # who am I logged in as?
pwd # where am I?
echo $PATH | tr ':' '\n' # what is on your $PATH? (one entry per line)
pwd should show your home directory, /home/users/SUNetID. If you’re somewhere else, run cd on its own to return home before continuing.
Step Into the Repo First
Your current directory determines where relative file paths point and where files are saved. Move into the repo you cloned earlier so the commands below use the right folder:
cd ~/yens-onboarding-2026
pwd # confirm: /home/users/SUNetID/yens-onboarding-2026
Relative paths are relative to where you are standing. Nearly every command in this course is written relative to the repo root, such as
python scripts/…andsbatch slurm/…. Run one from somewhere else and the shell looks in the wrong folder and reportsNo such file or directory. Check that you are in the repo root before trying again.When a file or command “doesn’t exist,”
pwdis the first thing to check.
$PATH lists the directories the shell searches for commands. It looks something like this:
/home/users/SUNetID/.local/bin # your SUNetID goes here
/usr/local/sbin
/usr/local/bin
/usr/bin
...
/zfs/tools/darc/bin
When you type a command, the shell walks these directories top to bottom and runs the first matching executable it finds.
Bonus: Using what you just learned, find
python3.
💡 Hint: find Python
Runwhich python3 and look at the front of your `$PATH`.
So typing python3 checks .local/bin/python3, then /usr/local/sbin/python3, and so on, stopping at the first hit.
Loading a Module Changes Your $PATH
module load python
echo $PATH | tr ':' '\n'
The module adds a directory to the front of the list:
/software/free/python/3.10.5/bin
Run which python3 again. It should now point to Python in the module’s directory.
Unload a Module
Unload the module to remove its directory from $PATH:
module unload python # or: module purge (unloads everything at once)
echo $PATH | tr ':' '\n' # the python module's bin/ is gone from the front
which python3 # back to the system python3 again
💡 This load / unload of
$PATHis the exact same mechanism you’ll meet in Python Environments: activating an environment prepends a directory to the front of your PATH, and deactivating removes it.
Step 2: Open a JupyterHub Terminal and Install the Packages
Switch from your SSH terminal to your web browser. JupyterHub runs in your browser, but its terminals and notebooks run on the Yens.
Choose a node and log in with your SUNetID:
Click the blue “+” to open the Launcher, then select Terminal. Keep this tab open for the rest of the exercise. JupyterHub and your SSH session access the same files, but this is a new terminal session, so move into the repo again.
Run these commands in JupyterHub’s Terminal tab. Install the packages before starting Python. At this point you should be at a shell prompt, not the Python
>>>prompt or a notebook cell.
cd ~/yens-onboarding-2026
python3 -m pip install matplotlib numpy
Wait for the installation to finish. Requirement already satisfied means a package
is already installed. Using python3 -m pip installs for the Python you will start with
python3 in this terminal.
| Label on a code block | Where to enter it |
|---|---|
| JupyterHub Terminal | The shell in JupyterHub’s Terminal tab |
| Python Prompt (»>) | The same Terminal tab after you start python3 |
| Jupyter Notebook Cell | A code cell in a notebook; run it with Shift+Enter |
Step 3: Run Python in the JupyterHub Terminal
You do not need a file to run Python. The interactive interpreter lets you type code straight into the terminal and run it immediately, one line at a time.
Stay in the same JupyterHub Terminal tab where you installed the packages. Start Python:
python3
The prompt changes to >>>. You are now inside Python, in the same Terminal tab. Paste the plotting code below at that prompt:
import matplotlib.pyplot as plt # plotting library
import numpy as np # scientific computing library
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
ax.set_title("My First Plot")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()
fig.savefig("my_plot.png", dpi=300, bbox_inches="tight")
plt.close(fig)
When you are done, leave the interpreter and return to the shell:
exit()
💡
plt.show()would normally pop open a window, but a terminal has no screen to draw on, so nothing appears. That is why we also callfig.savefig(...): it writes the plot tomy_plot.pngin your current directory.
💡 Help with ModuleNotFoundError
In the JupyterHub Terminal tab, leave Python withexit(). Run python3 -m pip install matplotlib numpy at the shell prompt, then start python3 again.
Look at What You Made
After exit(), you are back at the shell prompt in the JupyterHub Terminal tab:
pwd # where am I? this is where the file was written
ls # you should now see my_plot.png
cat my_plot.png # try to "read" the image
savefig wrote my_plot.png into your current directory. Since you started Python from the repo, the image should be there too. If you started from your home directory, look there instead.
cat prints something like:
�PNG
IHDR....IDATx...��KѐP....
A PNG is a binary image file, so cat cannot display it as readable text. In JupyterHub’s file browser, open yens-onboarding-2026/my_plot.png to see the plot.
🟢 Green sticky = the plotting code ran in the interpreter and
lsshowsmy_plot.pngin my repo folder 🔴 Red sticky = I got aModuleNotFoundError, or nomy_plot.pngappearedPut a sticky note on your laptop lid so instructors can see where you are.
Step 4: Open a Notebook Alongside the Terminal
- In JupyterHub’s file browser, open
yens-onboarding-2026so the notebook is saved in your repo - Click the blue “+” to open the Launcher
- Start a Python 3 notebook
- Keep the Terminal tab from Step 2 open
A notebook runs code in cells you execute one at a time, with the results (text, tables, even images) appearing right below each cell. The Terminal tab is where you installed packages and started the Python interpreter. Its Python can differ from the notebook’s kernel; you will connect both to the same environment in Python Environments. You’ll switch between the two throughout today.
Step 5: Run a Cell
Blocks labeled Jupyter Notebook Cell go in a notebook code cell. Blocks labeled JupyterHub Terminal go at the shell prompt in your Terminal tab.
Type this into the first cell and run it with Shift+Enter:
numbers = [1, 2, 3, 4, 5]
print(sum(numbers))
Expected output: 15
Now run the plotting code from Step 3 in a new cell:
import matplotlib.pyplot as plt # plotting library
import numpy as np # scientific computing library
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
ax.set_title("My First Plot")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()
The graph appears right below the cell. In a notebook, plt.show() displays the plot inline.
💡 In a notebook you don’t even need
fig.savefig(...)just to see a plot. You still save it when you want a file to keep, share, or hand to the cluster.
Step 6: Run the Same Code as a Script
You have now run Python two ways: the interactive interpreter (Step 3) and a notebook (Step 5). The third way is a script, a .py file that runs start to finish on its own. This is what you submit to the cluster.
- In the Launcher, open a Text File (or run
nano plotting_code.pyin the Terminal) and name itplotting_code.py. - Paste in the plotting code from Step 3, keeping the
fig.savefig("my_plot.png", ...)andplt.close(fig)lines so it writes the image to a file. - Return to the JupyterHub Terminal tab. If you see
>>>, runexit()first. Run the script from the repo root:
cd ~/yens-onboarding-2026
python3 plotting_code.py
The script creates the same plot. Notebooks are useful for exploration; scripts let you run the full sequence as a cluster job. For the rest of the course, you will write scripts.
Bonus
Finished early? Try any of these.
Bonus 1: Open the Saved Plot
In JupyterHub’s file browser, find my_plot.png and double-click it to open the image viewer.
Bonus 2: Edit the Plot
Edit the plotting cell in your notebook, then run it again with Shift+Enter:
- Plot a second line, e.g.
ax.plot([1, 2, 3, 4], [2, 3, 1, 4], label="second run") - Give each line a
label=...and addax.legend()to name them - Change a line’s color with
color="crimson"(or"teal","goldenrod")
Try editing the plot with Claude Code. First save your notebook into your repo folder with a name you’ll recognize (File → Save Notebook As…, e.g. yens-onboarding-2026/plotting.ipynb). Then open a Terminal in JupyterHub and start Claude Code as you did in Working with Claude Code:
cd ~/yens-onboarding-2026 # the folder holding your notebook
ml claude-code
claude
Start Claude Code from the repo folder so it has the notebook in its working directory.
Describe the changes you want to make to the plot.
💡 Example prompt for Claude
For example:
Edit the plot in plotting.ipynb to use bold colors, several lines, a title, annotations, and gridlines. Save the notebook so I can run it again.
Check the permission mode and model covered in Working with Claude Code.
Permission mode (
Shift+Tabcycles it; the current one shows at the bottom of the screen). For this practice plot, accept edits lets Claude change the file without asking about each edit. Use plan mode to review its proposed changes first, or manual to approve individual actions.Model (
/model). Use Sonnet or Haiku for a small plotting edit. Save Opus for more complex tasks. Stanford’s managed plan has a usage limit; if you reach it, you’ll need to wait for it to reset.
Claude edits the .ipynb file on disk. Switch back to the notebook tab and click Reload when JupyterHub reports that the file changed. Then choose Kernel → Restart Kernel and Run All Cells to see the updated plot.
Quiz
Answer each one in your head, then open it to check.
1You load a Python module, then run python3 again. Why might a different Python start?
The module changes the search order in $PATH. It adds its Python directory near
the front, so the shell finds that executable before the one it used previously.
Run which python3 before and after loading the module to compare the paths.
2Your plotting code saved my_plot.png, but you cannot find it in the repo. Where should you look?
Check the directory where you started Python. A relative filename such as
my_plot.png is saved in the current working directory. If you started Python from
your home directory, look there. After leaving Python, run pwd and ls to check.
Starting Python from ~/yens-onboarding-2026 keeps the exercise’s output in the repo.
3You run python3 plotting_code.py from the shell in your JupyterHub terminal on the Yens. The script finishes, but no plot window opens. Does that mean the script failed?
Not necessarily. In this terminal session, there is no graphical display for
plt.show() to open a window. Check whether fig.savefig(...) created my_plot.png,
then open that file in JupyterHub’s image viewer. In a notebook, the plot can appear
below the cell instead.
4You run a cell in the Yens JupyterHub from your laptop's browser. Which computer runs the Python code?
The Yen hosting your notebook kernel. Your laptop displays the interface and results.
The notebook uses the cluster’s computing resources and files, which is why you can see
the same my_plot.png through both JupyterHub and your SSH session.
5Your prompt shows >>>. Can you type ls or cd there to navigate?
You are inside Python, so shell commands do not work there. Run exit() to return
to the terminal’s shell, then use ls or cd. The prompt helps you check which program
is waiting for your input.
6You want to run the full plotting sequence again without entering each line or clicking notebook cells. What should you save?
A Python script, such as plotting_code.py. Save the code in that file, then run it
from the shell in the folder containing it:
python3 plotting_code.py
Python executes the script from top to bottom. Keep the fig.savefig(...) line so the
run produces an image file you can open afterward.
🧠 Skills Learned
- You can open JupyterHub on the Yens and run Python from any browser
- You know the difference between a notebook cell and a script, and you know when each is useful
- You can run a
.pyscript from the terminal, which is how cluster jobs work