Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Python環境配置
開始前的Python知識
Python是一種 "直譯式"、"高級程式語言",在1991年,由工程師吉多·范羅蘇姆所開發,現由Python軟體基金會管理。
由於其程式語法的易讀性及編程快速的優點,現多被用於資料科學以及人工智慧的程式開發,也是很多程式初學者的優先選擇。
更多詳細資訊,請參考Python Docs - Beginner's Guide to Python。
系統選擇
Python可以在Windows、Mac OS或者Linux等多個平台上執行及編譯。
Mac OS
Mac OS上有很多的Python編譯器可以選擇,像是Visual Studio Cdoe、Spyder等,但是建議初學者使用像是Anaconda或Xcode這類的整合式開發環境(IDE)會比較方便。
更多詳細資訊,請參考Python Docs - 在 Mac 系統使用 Python。
註:Mac OS其實有是原生內建Pyhton的,只是版本可能比較舊,建議如果要使用Mac OS來寫Python程式的話,在開始的配置還是去官網下載一下for mac的安裝檔更新會比較好
Windows
Windows上的Python就相對開放許多(至少就很多奇怪的插件都不會被阻擋),在這個方面而言,Window也許更符合您需求。
Windows上也有很多IDE可供選擇,例如Visual Studio、PyCharm或Atom。前面Mac OS提過的Anaconda這個整合式開發環境也有,種類繁多。
註:由GitHub開發的Atom編輯器,已於Dec/12/2022停止更新,因此不建議使用
更多詳細資訊,請參考Windows Docs - 開始在 Windows 上使用適用於初學者的 Python。
註:從Python 3.9開始,官方便不支援在Windows 7及以下版本中安裝和執行,建議優先選擇Windows 10以上的版本。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Python環境配置
這邊以Windows 11以及Visual Stidio Code做安裝示範:
1. 安裝Python
-
前往 Python
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
,選擇 "Download Python 3.10"
註:小數點為例行性更新,直接下載即可
-
安裝Python,選擇 "Install Now"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
註:Add Python 3.10 to PATH 記得要勾上
2. 安裝Visual Studio Code
-
前往VS Code
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
,選擇 "Download for Windows"
-
安裝VS Code,全部選同意;有其它選項的,照以下圖片操作:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
這邊不建議去改變安裝位置,避免出現奇奇怪怪的Bug
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
這邊選下一步即可
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
建議全部選上,方便後續操作,接著重新開機
3. 設置Visual Studio Code
-
完成安裝之後開啟Visual Studio Code,應該會出現以下畫面,游標滑到左半邊,點選下面的 "Mark Done" 即可;右邊是換主題用的,稍後做示範
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
安裝繁體中文插件,點擊右邊的四個框框,輸入 "traditional",選擇第一個,點擊 "Install"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
安裝完成會需要重新啟動VS Code,右下角會跳出提示框,點"立即重新啟動"即可
-
安裝Python插件,輸入 "Python",選擇第一個,點擊 "安裝"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
安裝完Python,可能會跳出要配置Jupyter Notebooks的視窗,一樣滑到下面點擊"標記完成"即可
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
1. 開啟資料夾及新增Python檔案
-
找一個地方建立一個專門用來放程式的資料夾,像是在桌面建立一個"Python_files"資料夾
資料夾的命名,如需連接兩個字,建議用下引號" _ " 進行連接
-
在資料夾上點擊右鍵,選擇 "顯示其他選項",選擇 "以Code開啟"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
勾選 "信任父資料夾 '資料夾位置' 中所有檔案的作者",點擊 "是,我信任作者 信任資料夾並啟用所有功能"
註:資料夾位置取決於您於哪個地方建立此資料夾,例如桌面就是'Desktop'
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
此操作僅須在新建資料夾後,第一次開啟時執行
2. 第一支Python程式
-
移至最左邊的第一個文件選項,點擊 "新增檔案"
-
新增一個檔案,副檔名為 ".py"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
新增檔案按紐,在滑鼠游標靠近時會自動出現
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
在右邊編輯區塊輸入
-
點擊右上角三角形執行鈕執行,查看Terminal返回值
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
如果出現圖片所顯示的Terminal返回值,恭喜您,已經完成了第一隻Python程式的編譯
-
儲存檔案
-
VS Code上,未儲存的檔案,會在檔案名稱旁邊顯示一個點
按 "Ctrl"+"S" 儲存
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
每次離開VS Code前都記得要儲存
雖然VS Code在每次執行時,都會自行存檔(Mac好像需要設定才會),還是建議離開前按一下 "Ctrl" + "S"
-
儲存完檔案,在資料夾裡面會多出一個長這樣的Python文件(下圖),此Python已經可以直接執行,也可以 "以Code開啟" 來編輯
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
註:檔案開啟之後,出現一個黑框框然後馬上跳出,是正常的,因為指令執行完之後,程序就會自動退出,除非有特別去寫要暫停幾秒
3. 使用pip安裝模組 (module)
pip是一個專門設計給Python使用的軟件包管理系統,其中最常用的pip功能有 "install"、"uninstall" 及 "list";而pip的用方法非常簡單,就是 "pip" + "功能"
1. 以selenium為例,安裝模組
-
點擊左上角的"終端機",選擇"新增終端"
-
在Terminal(終端機)中輸入 "pip install" + module name
註:記得空格
-
等待Terminal返回 "Successfully installed"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
按 "F1",輸入 "reload window",按Enter重新啟動頁面,完成安裝
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
2. 檢查已安裝的module
- 在終端機輸入以下指令即可檢查已安裝模組
- 如須清除terminal,則輸入cls即可
3. 以selenium為例,解除安裝module
- 如需解除安裝module,在終端機輸入 "pip uninstall" + module name
註:記得空格
-
然後終端機會返回要使用者確認的指示,輸入 "Y" 再按Enter即可

4. 以selenium為例,更新module
Python的module資料庫相當龐大,常常會有新的更新出現,故更新module也因此成為了學習Python必修的技能之一
註:但除非有重大的功能性更新或是Bug修復,不然還是不要太常更新,容易導致程式碼出錯;因為新的module可能會換語法,而使用者尚未了解
- 在Terminal(終端機)中輸入 "pip install –update"+module name
註:記得空格
4. 新增系統變數 (PATH)
PATH 是一個系統變數,可讓您的作業系統從指令行或終端機視窗中尋找所需的可執行檔。
JAVA - 我要如何設定或變更 PATH 系統變數?
-
將解壓縮完的資料夾移動到指定資料夾
如果沒有特殊需求,建議將需要設定為PATH的資料夾拖放到C夾,這樣在設定PATH及日後更新會更方便
-
按 Windows鍵,輸入 "編輯系統環境變數"

-
選擇 "環境變數"

-
選擇上面的 "Path",選擇 "編輯"

-
選擇 "新增",選擇 "瀏覽",找到剛才存放資料夾的位置;或是直接輸入存放資料夾的位置
如果按照上的說明操作,位置為:C:\chromedriver_win32
-
按確定,接著重新啟動,完成PATH的設定
5. 變更主題及字型大小
Visual Studio Code的主題可以變更為預設的 "深色" 或是 "淺色",也可以變更為其他開發者開發的主題,而其形式便是以插件的型態供使用者安裝及使用
1. 變更字型大小
-
點擊左下角的齒輪圖標,選擇設定

-
輸入 "font size"

-
變更第三個 "Editor:Font Size" 的數值,即可改變編譯器字型大小

2. 變更內建主題
-
點擊左下角的齒輪圖標,選擇 "色彩佈景主題"

-
上方會出現選項供使用者更改

3. 變更為使用者自訂主題
前面提過,第三方的主題包,以插件的形式供使用者安裝,故安裝方式與前面安裝Python插件大同小異
-
點擊右邊的四個框框,輸入想要的主題 (例如:Pink)
-
選擇目標主題包,安裝,套用即可

6. 錯誤排查 (Debug)
寫程式的過程,免不了Debug的過程;而程式出錯的原因,大致上可以分成三種;Visual Studio Code其實會提示出錯的原因和建議的修正方式,只通常只適用於預設的語法錯誤,如果是因為module的語法或是引用出現錯誤,就只會提示 "Index Error"
1. 出現錯誤後,Visual Studio Code會怎麼提示 (僅限語法錯誤)
-
程式的名稱會變成黃色
註:右邊的黑點代表尚未儲存

正常的程式碼名稱會是黑色的

-
出現錯誤的程式碼片段底下,會出現黃色波浪線

-
下面終端機,最左邊的 "問題" 旁邊會出現數字
註:數字代表已發現的錯誤數量

2. 怎麼判斷是哪裡出錯
如前面所述,錯誤可以大致分成三種,此三種分別是 "邏輯錯誤"、"語法錯誤" 或者很令人難以理解的 "當機"
1. 邏輯錯誤
邏輯錯誤,常見的表現是:當你寫完一段程式碼,並且順利執行了,可是回傳的值卻不是你所預期的,並且沒有語法或是執行次序上的錯誤
2. 語法錯誤
語法錯誤,例如print打成prinf,這個語法錯誤會在VS Code上面直接顯示出來,通常terminal也會提示出建議的修正方法
此時terminal會提示 "Did you mean: 'print'?",然後問題會顯示 "'prinf' is not defined"
3. 當機
這種錯誤都出現的很突然,並且難以復現;但是檢查以下幾個重點:
都符合的話,那恭喜你,一定是當機了
3. 怎麼修正錯誤
1. 針對邏輯錯誤
舉例:求三角形面積,假設底邊長8cm,高4cm,兩邊長分別為5cm及根號41cm,答案應該是16平方公分;而答案卻返回了20平方公分
terminal回傳值:20
這便是典型的邏輯錯誤,因為三角形間應該是"(底x高)/2",而不是"(底x邊/2)"
所以應該改成
terminal回傳值:16
這樣程式的修正便完成了
2. 針對語法錯誤
以前面舉例的prinf為例,VS Code已經給出了建議 "print",那通常照建議去修改就沒問題了;若是提示Index Error,那可以把報錯的那行複製起來去Google,通常都會是因為沒有按照module的語法編寫或是打錯字
3. 針對當機
造成當機的原因有很多種,可能是過熱、太久沒關機或者是記憶體滿了;而這種當機最煩的就是 "不是卡死" 也不是 "藍屏",很多時候都是查來查去都找不出來,到最後重新開機就解決了,所以面對當機,只有下兩種解決辦法
-
先儲存資料,然後按 "F1",然後輸入 "reload window",按Enter重新啟動VS Code

-
如果重新啟動VS Code還沒辦法解決問題的話,那就趕快儲存資料,然後重新開機
Authored by Liyue-Wei on GitHub