Jupyter Notebook vs Python Script: When To Use .ipynb and When To Use .py
Quick Answer
Use Jupyter notebooks for exploration, learning, data analysis, and visual explanations.
Use Python scripts for reusable programs, automation, apps, tests, and production-style code.
Last verified: June 9, 2026. JupyterLab documentation currently shows the 4.5.0 documentation series.
Official Links
- Jupyter install: https://jupyter.org/install
- JupyterLab installation docs: https://jupyterlab.readthedocs.io/en/stable/getting_started/installation.html
- VS Code Jupyter docs: https://code.visualstudio.com/docs/languages/python#_jupyter-notebooks
- VS Code Jupyter extension: https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
What You Will Learn
By the end, you should be able to:
- explain notebooks and scripts
- install JupyterLab on Windows and macOS
- launch JupyterLab
- select the right notebook kernel
- fix package import problems
- decide when to use
.ipynbvs.py - convert notebook thinking into script thinking
1. What Is A Jupyter Notebook?
A Jupyter notebook is a .ipynb file made of cells.
Cells can contain:
- Markdown notes
- Python code
- tables
- charts
- outputs
It is excellent for learning because code and explanation sit together.
2. What Is A Python Script?
A Python script is a .py file.
It runs from top to bottom:
python script.pyScripts are better for repeatable work because they are easier to test, review, schedule, and package.
3. Install JupyterLab Safely
Use a virtual environment.
Windows:
mkdir jupyter-practice
cd jupyter-practice
py -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install jupyterlab pandas matplotlibmacOS:
mkdir jupyter-practice
cd jupyter-practice
python3 -m venv .venv
source .venv/bin/activate
python -m pip install jupyterlab pandas matplotlibLaunch:
jupyter labIf jupyter lab is not found, use:
python -m jupyterlabIf the browser does not open automatically, look in the terminal for a local URL that starts with:
http://localhost:Copy that full URL into your browser. It may contain a token, which proves the browser session belongs to your local Jupyter server.
The official Jupyter page also documents classic Notebook:
python -m pip install notebook
jupyter notebookFor new learners, JupyterLab is usually the better default.
4. Jupyter In VS Code
Install:
- Python extension
- Jupyter extension
Open a .ipynb file and select the kernel connected to your .venv.
If VS Code asks for a kernel:
- Choose Python Environments.
- Select the
.venvinterpreter. - If missing, install
ipykernel.
python -m pip install ipykernel5. Notebook Kernel vs Terminal Python
A notebook kernel is the Python process running your notebook.
Your terminal may use:
project/.venv/bin/pythonbut your notebook may use:
some-other-pythonThat causes import errors.
Check inside a notebook cell:
import sys
print(sys.executable)It should point to your project .venv.
6. Notebook Example
Create a notebook and run:
import pandas as pd
scores = pd.DataFrame({
"student": ["Asha", "Ben", "Chen"],
"score": [88, 91, 79],
})
scoresThen:
scores["score"].mean()This style is good for exploration because you can inspect intermediate results.
7. Script Example
Create analyze_scores.py:
import pandas as pd
def main():
scores = pd.DataFrame({
"student": ["Asha", "Ben", "Chen"],
"score": [88, 91, 79],
})
print(scores)
print("Average:", scores["score"].mean())
if __name__ == "__main__":
main()Run:
python analyze_scores.pyThis style is better when you want to rerun the same logic reliably.
8. When To Use Notebooks
Use notebooks for:
- learning a concept
- data exploration
- quick charts
- explaining analysis step by step
- experimenting with models
- sharing educational walkthroughs
9. When To Use Scripts
Use scripts for:
- command-line tools
- scheduled jobs
- reusable functions
- tests
- web apps
- data pipelines
- anything you must run the same way repeatedly
10. Common Notebook Mistakes
Running cells out of order
Notebook state can hide bugs. Use Restart Kernel and Run All before trusting results.
Package installed but import fails
Install into the notebook kernel:
import sys
!{sys.executable} -m pip install package-nameExplanation
- This code snippet demonstrates how to install Python packages dynamically during runtime by executing pip commands through the system's Python interpreter
- The sys.executable variable ensures the installation targets the correct Python environment where the script is running
- The ! prefix indicates this is meant to run in a Jupyter notebook environment where shell commands can be executed directly
- This approach is useful for automated setup scripts or when packages need to be installed conditionally based on runtime conditions
- The command structure allows for programmatic package management without requiring manual terminal intervention
For normal project work, prefer installing from the terminal after activating .venv.
Saving huge outputs
Large outputs make notebooks slow. Clear outputs before committing notebooks to Git.
Treating notebooks like production apps
Move reusable logic into .py files when code becomes serious.
11. Convert Notebook Thinking Into Script Thinking
Notebook:
load data
inspect data
clean one issue
chart something
try another ideaScript:
load_data()
clean_data()
build_report()
main()The notebook helps you discover the workflow. The script makes it repeatable.
Decision Table
| Situation | Use |
|---|---|
| Learning pandas | Notebook |
| Building a CLI tool | Script |
| Creating charts for exploration | Notebook |
| Creating a daily report pipeline | Script |
| Teaching a data concept | Notebook |
| Writing tests | Script |
FAQ
Is Jupyter only for data science?
No, but it is most useful for data analysis, visualization, teaching, and experimentation.
Can I use notebooks in VS Code?
Yes. Install the Python and Jupyter extensions, then select the correct kernel.
Why does my notebook remember old variables?
The kernel keeps state until restarted. Use Restart Kernel and Run All to check if the notebook is truly reproducible.
Should I upload .ipynb or .py for projects?
For learning projects, notebooks are useful. For production-style projects, provide scripts too.
Final Checklist
Windows:
py -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install jupyterlab ipykernel pandas
jupyter labmacOS:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install jupyterlab ipykernel pandas
jupyter labInside a notebook:
import sys
print(sys.executable)Explanation
- The code imports the sys module which provides access to system-specific parameters and functions
- It uses sys.executable to retrieve the path to the Python interpreter currently running the script
- The print function outputs this path to the console, showing where Python is installed or being executed from
- This is commonly used for debugging purposes or when you need to verify which Python environment is being used
- The output typically shows the full file path like /usr/bin/python3 or C:\Python39\python.exe depending on the system
If the path contains .venv, your notebook is using the right Python.
