Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Windows 綜合教學 >> 關於 Windows

Windows 7 執行 .py (Python) 檔案完整環境建置教學

編輯:關於 Windows

問題背景

Windows 7 預設雙擊 .py 檔案以「記事本」開啟,無法直接執行。需安裝 Python 解譯器並建立關聯,才能像 Linux/macOS 一樣直接運行腳本。

安裝步驟

  1. 前往 Python 官網下載頁,下載最新 Windows x86-64 executable installer (64 位元) 或 x86 (32 位元)。建議勾選「Add Python to PATH」選項。
  2. 執行安裝程式 → 勾選 「Add Python 3.x to PATH」 (關鍵!) → 「Install Now」或「Customize installation」→ Next → Install。
  3. 安裝完成後,命令提示字元輸入 python --version 或 py --version 驗證。

圖片資訊

Python 下載頁面

安裝精靈勾選Add to PATH

安裝完成後py檔案圖示變化

驗證與使用

  • 安裝完成後,所有 .py 檔案圖示自動變為 Python 圖示 (火箭/蛇)。
  • 雙擊 .py 檔案即可直接執行 (主控台視窗顯示輸出,執行結束視窗自動關閉)。
  • 若需保留視窗查看結果,腳本末尾加入 input("Press Enter to exit...") 或建立批次檔 python script.py & pause。
  • 命令列執行:python script.py 或 py script.py (py launcher 自動選擇版本)。

進階建議

  • 虛擬環境:專案開發請使用 python -m venv venv 建立隔離環境,避免套件衝突。
  • 套件管理:pip install package_name 安裝第三方套件 (requests, numpy, pandas 等)。
  • 編輯器:推薦 VS Code (含 Python 擴充套件)、PyCharm Community、Sublime Text + 插件。
  • 版本管理:安裝多版本 Python 時,py -3.10 script.py 指定版本運行。

學習資源推薦

Python 語法簡潔、標準庫豐富、社群龐大。零基礎入門約 1-2 小時可掌握核心語法。推薦資源:

  • 官方教學:Python Tutorial (繁體中文)
  • 實戰專案:Automate the Boring Stuff with Python (免費線上閱讀)
  • 中文社群:PTT Python 版、Python Taiwan Discord、Facebook 社團
Copyright © Windows教程網 All Rights Reserved