Jupyter Notebook 是一個網頁版的互動式程式開發環境,特別適合資料分析、機器學習和教學使用。
💡 為什麼要用 Jupyter?
相比傳統的 .py 檔案,Jupyter Notebook 讓你可以邊寫邊測試,特別適合學習和實驗!
# Windows: 開啟命令提示字元 (cmd) # macOS/Linux: 開啟終端機 (Terminal) python --version # 或 python3 --version # 應該顯示類似: Python 3.11.0
⚠️ 如果顯示「找不到命令」:
表示還沒安裝 Python,請先到 python.org 下載並安裝
Windows: 按 Win + R,輸入 cmd,按 Enter
macOS: 按 Cmd + Space,輸入 Terminal
python -m pip install --upgrade pip
pip install notebook
等待安裝完成,通常需要 1-3 分鐘
jupyter notebook --version
應該顯示版本號,例如: 7.0.6
Anaconda 是一個Python 資料科學套件包,已經包含 Jupyter Notebook 和常用的資料分析套件 (NumPy, Pandas, Matplotlib 等)。
💡 優點: 一次安裝所有常用套件,不用個別安裝
缺點: 檔案較大 (約 3 GB),安裝時間較長
在您想要建立筆記本的資料夾位置開啟命令列
jupyter notebook
瀏覽器會自動開啟,顯示 Jupyter 介面
網址通常是: http://localhost:8888
⚠️ 注意: 不要關閉命令列視窗!關閉後 Jupyter 就會停止運作
✅ 成功啟動的標誌:
命令列會顯示 "The Jupyter Notebook is running at: http://localhost:8888/"
Shift + Enter 執行程式碼# 在第一個 Cell 輸入: print("Hello, Jupyter!") # 按 Shift + Enter 執行 # 應該會在下方顯示: Hello, Jupyter!
🎉 恭喜! 您已經成功建立並執行第一個 Jupyter Notebook!
Shift + Enter - 執行當前 Cell 並移到下一個Ctrl + Enter - 執行當前 Cell 但不移動Alt + Enter - 執行當前 Cell 並在下方插入新 CellA - 在上方插入新 Cell (命令模式下)B - 在下方插入新 Cell (命令模式下)D + D - 刪除當前 Cell (按兩次 D)M - 將 Cell 轉換為 Markdown (文字說明)Y - 將 Cell 轉換為 Code (程式碼)Enter - 進入編輯模式 (綠色邊框)Esc - 進入命令模式 (藍色邊框)💡 提示: 在命令模式下按 H 可以查看完整的快捷鍵列表
# 在 Jupyter Cell 中執行: !pip install pandas numpy matplotlib # 驗證安裝 import pandas as pd print(pd.__version__)
# 先關閉 Jupyter,在命令列執行: pip install pandas numpy matplotlib seaborn openpyxl # 然後重新啟動 Jupyter jupyter notebook
在 Jupyter Notebook 中執行以下程式碼:
# 匯入 Pandas import pandas as pd # 建立範例資料 data = { '姓名': ['張三', '李四', '王五'], '年齡': [25, 30, 35], '城市': ['台北', '台中', '高雄'] } # 建立 DataFrame df = pd.DataFrame(data) # 顯示資料 print(df) # 在 Jupyter 中,也可以直接輸入變數名稱來顯示 df
✅ 如果看到漂亮的表格顯示,恭喜!
您的 Jupyter Notebook 和 Pandas 已經完全設定好了!
# 錯誤: 'jupyter' 不是內部或外部命令 # 解決方法: 重新安裝 pip uninstall notebook pip install notebook
手動複製命令列中顯示的網址 (例如 http://localhost:8888/?token=xxx) 並貼到瀏覽器
# 錯誤: ModuleNotFoundError: No module named 'pandas' # 解決方法: 在 Jupyter Cell 中安裝 !pip install pandas
點選上方選單: Kernel → Restart Kernel
⚠️ 還是無法解決?
嘗試重新啟動電腦,或到課程社團發問!
Ctrl + S (Windows) 或 Cmd + S (Mac).ipynb 格式點選上方選單: File → Download as
# 顯示執行時間 %time print("Hello") # 顯示所有變數 %whos # 在 Cell 中執行系統指令 !ls # 列出檔案 (macOS/Linux) !dir # 列出檔案 (Windows)
# 讓圖表顯示在筆記本中 %matplotlib inline import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4]) plt.show()
Tab 鍵自動完成變數名稱或函數? 查看說明: pd.read_csv?Shift + Tab 顯示函數參數提示pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user
🎉 恭喜完成 Jupyter Notebook 設定!
現在您已經準備好開始 LangChain AI 資料分析課程了!
💡 下一步:
前往 Unit 0-2 學習中文化設定,或直接進入 Unit 1 學習 LangChain!