> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Runner

> 在專案中執行 Python 指令碼，並儲存它寫出的內容，例如要用在文件中的圖表。

Python 指令碼在你的瀏覽器中執行，因此輸出會直接顯示在編輯器下方。無需任何設定。

**1. 建立指令碼**

在檔案樹中，按一下 **New file** 圖示。輸入以 `.py` 結尾的名稱，然後按一下 **Create**。

![名稱為 analysis.py 的 Add Files 對話方塊](https://mintcdn.com/ayakaleaf-pro/PeD9iteXIf8j4GXw/images/on-premises/python-runner-new-file.png?fit=max\&auto=format\&n=PeD9iteXIf8j4GXw\&q=85\&s=6be49974e488f7678604cc0644f43bdc)

新的 Python 檔案

編輯器下方會出現 **Run** 按鈕和輸出窗格。第一次使用時，在 Python 載入期間該按鈕會維持停用狀態。

**2. 執行指令碼**

撰寫程式碼並按一下 **Run**。指令碼列印的內容會顯示在輸出窗格中。不需要先儲存。

指令碼寫出的檔案會存入你的專案。一則訊息會告訴你是哪個檔案，例如 **results.txt saved to your project**，該檔案也會出現在檔案樹中。

![編輯器中的指令碼、下方列印的輸出，以及訊息 results.txt saved to your project](https://mintcdn.com/ayakaleaf-pro/PeD9iteXIf8j4GXw/images/on-premises/python-runner-output.png?fit=max\&auto=format\&n=PeD9iteXIf8j4GXw\&q=85\&s=28188ffe214aedd1203d2734891917f7)

輸出和已儲存的檔案

**3. 停止指令碼**

指令碼執行時，**Run** 會變成 **Stop**。按一下即可中斷指令碼。輸出窗格會以 **Execution interrupted** 結尾。

![Stop 按鈕和正在執行的指令碼的輸出](https://mintcdn.com/ayakaleaf-pro/PeD9iteXIf8j4GXw/images/on-premises/python-runner-stop.png?fit=max\&auto=format\&n=PeD9iteXIf8j4GXw\&q=85\&s=263af195efa53538f1e3b37b4fad6041)

正在執行的指令碼

## 在文件中使用圖表

繪圖沒有可以顯示的視窗，因此請將其儲存為檔案。例如：

```python theme={null}
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 9])
plt.savefig("figure.png")
```

`figure.png` 會出現在你的專案中，可直接用於 `\includegraphics{figure.png}`。

![在編輯器中開啟的 figure.png，以及訊息 figure.png saved to your project](https://mintcdn.com/ayakaleaf-pro/PeD9iteXIf8j4GXw/images/on-premises/python-runner-figure.png?fit=max\&auto=format\&n=PeD9iteXIf8j4GXw\&q=85\&s=8b0074f8d46a40a105a459a6c09caabf)

已儲存的繪圖

## 讀取專案中的檔案

指令碼可以依路徑開啟專案中的檔案：

```python theme={null}
print(open("results.txt").read())
```

## 發生問題時

**指令碼引發錯誤。**錯誤會以紅色顯示在輸出窗格中，位於指令碼先前列印的所有內容之後。

![列印的文字後接一段紅色的 traceback](https://mintcdn.com/ayakaleaf-pro/PeD9iteXIf8j4GXw/images/on-premises/python-runner-error.png?fit=max\&auto=format\&n=PeD9iteXIf8j4GXw\&q=85\&s=89ef60d790737fe01d6d69b1e0395e9e)

指令碼中的錯誤

**`ModuleNotFoundError: No module named ...`。**只能使用 Pyodide 內建的套件，無法使用以 `pip` 安裝的套件。NumPy 和 Matplotlib 都在可用之列，該訊息也會連結到[完整清單](https://pyodide.org/en/stable/usage/packages-in-pyodide.html)。

![輸出窗格中的 ModuleNotFoundError](https://mintcdn.com/ayakaleaf-pro/PeD9iteXIf8j4GXw/images/on-premises/python-runner-missing-package.png?fit=max\&auto=format\&n=PeD9iteXIf8j4GXw\&q=85\&s=5dbbfca95ee2b6984e2b3220e500231a)

無法使用的套件


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.