# 設定Linux系統中預設的Python版本 在 Rocky Linux 9 環境下,要將系統預設的 `python` 和 `python3` 指向 Python 3.9,你可以使用 `alternatives` 這個工具來管理系統中的符號連結。以下是詳細的步驟: 1. 檢查目前 Python 的指向 首先,先確認目前 python 和 python3 這兩個命令指向哪個 Python 版本。在終端機中執行以下指令: ```Bash ls -l /usr/bin/python* ``` 這個指令會列出所有以 python 開頭的可執行檔及其連結。你會看到類似以下的輸出(實際輸出可能因你的系統配置而有所不同): ``` lrwxrwxrwx. 1 root root 10 4月 26 11:00 /usr/bin/python -> python3.12 lrwxrwxrwx. 1 root root 10 4月 26 11:00 /usr/bin/python3 -> python3.12 -rwxr-xr-x. 1 root root 17120 3月 15 05:23 /usr/bin/python3.9 -rwxr-xr-x. 1 root root 17336 4月 5 09:15 /usr/bin/python3.12 ``` 從上面的範例中可以看到,目前的 python 和 python3 都指向 python3.12。 2. 使用 `alternatives` 設定預設 Python 版本 alternatives 指令可以讓你管理同一種功能的多個命令版本。我們需要將 python 和 python3 加入 alternatives 的管理,並設定 Python 3.9 為預設版本。 * 設定 python 的預設版本: 執行以下指令將 /usr/bin/python3.9 加入 python 的 alternatives,並設定一個較低的優先級(例如 10),然後將 /usr/bin/python3.12 加入並設定一個較高的優先級(例如 20)。這樣,當你選擇預設版本時,可以更容易地選到 Python 3.9。 ```Bash sudo alternatives --install /usr/bin/python python /usr/bin/python3.9 10 sudo alternatives --install /usr/bin/python python /usr/bin/python3.12 20 ``` 現在,使用以下指令來配置 python 的預設版本: ```Bash sudo alternatives --config python ``` 你會看到一個互動式的選單,列出可用的 Python 版本及其優先級。選擇對應於 `/usr/bin/python3.9` 的選項編號,然後按下 Enter。 * 設定 python3 的預設版本: 同樣地,我們也需要設定 python3 的預設版本。執行以下指令將 /usr/bin/python3.9 加入 python3 的 alternatives,並設定一個較低的優先級,然後將 /usr/bin/python3.12 加入並設定一個較高的優先級。 ```Bash sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 10 sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 20 ``` 現在,使用以下指令來配置 python3 的預設版本: ```Bash sudo alternatives --config python3 ``` 你會看到另一個互動式的選單,列出可用的 Python 3 版本及其優先級。選擇對應於 /usr/bin/python3.9 的選項編號,然後按下 Enter。 3. 驗證設定 設定完成後,再次檢查 python 和 python3 的指向以及它們的版本: ```Bash ls -l /usr/bin/python* python --version python3 --version ``` 現在,你會看到 /usr/bin/python 和 /usr/bin/python3 都指向 /etc/alternatives/python 和 /etc/alternatives/python3,而這兩個符號連結會指向你選擇的 Python 3.9 的實際路徑。同時,python --version 和 python3 --version 的輸出應該都會顯示 Python 3.9 的版本號。 ## 注意事項: 修改系統預設的 Python 版本可能會影響到一些依賴特定 Python 版本的系統工具或應用程式。在進行更改之前,請確保你了解可能帶來的影響。 對於開發專案,強烈建議使用虛擬環境(如 venv 或 conda)來管理不同專案所需的 Python 版本,而不是直接修改系統預設的 Python。這樣可以避免不同專案之間的依賴衝突。 透過以上步驟,你應該能夠成功將 Rocky Linux 9 環境下的預設 python 和 python3 設定為 Python 3.9。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up