致初學者:Python環境設置起手式
tags: Python
, Virtual Environment
English Version:
https://jason19970210.medium.com/to-beginners-learning-with-minimum-python-environment-setup-d37a6cdc6d3d
目前在開發實務上有著不同的編輯器 (Editor) (e.g. Visual Studio Code (VSCode), Sublime, Atom, Notepad++)、集成開發環境 (IDE, Integrated Development Environments) (e.g. PyCharm, Visual Studio)、網頁型態環境 (Web-Based) (e.g. Colab),讓大家更方便的使用 Python,但因為種類、方法繁多,也讓許多人產生疑惑
那麼最快上手的環境是哪一個?我自己覺得是 Colab,僅僅只需要打開一個分頁;但如果還是希望在自己的設備上,又該怎麼設置呢?
我們會先分類作業系統,進行不同的操作,以下是較為推薦的方式:
- Windows: 下載 Python 安裝程序
( link: https://www.python.org/downloads/ )
- macOS: 已內建包含 Python3 的執行環境
- Linux: 可以使用 apt 或 yum 套件管理器下載
example:
## Linux (with sudo privilege)
$ sudo apt install python3 python3-venv
在 Windows 安裝精靈中,請勾選 “Add python.exe to PATH (將 python.exe 加入環境變數)” 選項,在安裝完成後的頁面中,點擊 “Disable path length restrictions (禁用路徑長度限制)” 選項
"Add python.exe to PATH"
"Disable path length limit"
接下來我們要確認安裝是否完成,Python 能否正常運作:
注意事項:
在 Windows 環境中,通常是用 python 這個指令來執行 python v3 版本;而在 macOS、Linux 系統中,使用 python3 指令來對應 python v3 版本,python 指令則是對應 python v2 版本
## [Windows] Command Prompt (CMD) / PowerShell (PS)
C:\Users\user> python --version
Python 3.11.3
## macOS / Linux
user@localhost $ python3 --version
Python 3.11.3
確認安裝、執行沒有問題後,就要來開始建立工作目錄,在國外的文章、論壇中通常稱作 Directory
,也就是我們熟知的 Folder
資料夾
在這邊我們用「桌面 (Desktop)」舉例,並建立一個專案名稱為「hello_world」的資料夾
## [Windows] Command Prompt (CMD) / Powershell (PS)
C:\Users\user> cd Desktop
C:\Users\user\Desktop> mkdir hello_world
## macOS / Linux
user@localhost:~ $ cd Desktop
user@localhost:~/Desktop $ mkdir hello_world
對於每個專案所需要的套件 (dependency) 都會有所不同,因此我們需要建立 venv(虛擬環境, Virtual Environment)來達到區隔的效果
註:
在指令$ python3 -m venv venv
中,代表的事情是:讓電腦用python3
去執行,執行的內容是指定執行的模組 (module) 為venv
,並將參數帶入,而這邊的參數係指我們要建立的虛擬環境名稱
## [Windows] Command Prompt (CMD) / PowerShell (PS)
C:\Users\user\Desktop> cd hello_world
C:\Users\user\Desktop\hello_world> python -m venv venv
## macOS / Linux
user@localhost:~/Desktop $ cd hello_world
user@localhost:~/Desktop/hello_world $ python -m venv venv
此時系統就會在 hello_world 工作目錄中建立一個名為 venv 的新文件夾,裡面存放著跟虛擬環境相關的檔案,下一步就是啟動我們的虛擬環境
## [Windows] Command Prompt (CMD)
C:\Users\user\Desktop\hello_world> .\venv\Scripts\activate.bat
## [Windows] PowerShell (PS)
PS C:\Users\user\Desktop\hello_world> .\venv\Scripts\Activate.ps1
## macOS / Linux /* NOTICE: Check & set the exec permission */
user@localhost:~/Desktop/hello_world $ sudo chmod +x ./venv/bin/activate
user@localhost:~/Desktop/hello_world $ source ./venv/bin/activate
註:
在 Windows 環境中的 PowerShell 環境中,必須先設定ExecutionPolicy
執行權限,才能執行ps1
檔案
ref: https://superuser.com/a/106362
當我們成功啟動 venv 環境後,終端介面 (CLI, Command Line Interface) 的前綴會加上 venv 的名稱
## [Windows] Command Prompt (CMD)
(venv) C:\Users\user\Desktop\hello_world>
## [Windows] PowerShell (PS)
(venv) PS C:\Users\user\Desktop\hello_world>
## macOS / Linux
(venv) user@localhost:~/Desktop/hello_world $
### To deactivate ###
## [Windows] Command Prompt (CMD) / PowerShell (PS)
(venv) > deactivate
>
## macOS / Linux
(venv) $ deactivate
$
接著我們要檢查是否有確實用虛擬環境的 Python 執行檔來做執行
## [Windows] PowerShell (PS)
(venv) PS C:\Users\user\Desktop\hello_world> Get-Command python
(venv) PS C:\Users\user\Desktop\hello_world> gcm python
CommandType Name Version Source
----------- ---- ------- ------
Application python.exe 3.11.31... C:\Users\user\Desktop\hello_world\venv\Scripts\python.exe
(venv) PS C:\Users\user\Desktop\hello_world> Get-Command pip
(venv) PS C:\Users\user\Desktop\hello_world> gcm pip
CommandType Name Version Source
----------- ---- ------- ------
Application pip.exe 0.0.0.0 C:\Users\user\Desktop\hello_world\venv\Scripts\pip.exe
## macOS / Linux
(venv) $ which python3
/home/user/Desktop/hello_world/venv/bin/python3
(venv) $ which pip3
/home/user/Desktop/hello_world/venv/bin/pip3
下一步,在工作目錄中建立一個 main.py
來測試,Linux & macOS 環境下可以用 touch
指令新增檔案,再透過 vim
指令編輯檔案
Linux 安裝 vim 指令:
$ sudo apt install vim -y
$ sudo yum install vim -y
# filename: main.py
print("hello world")
## [Windows] Command Prompt (CMD) / PowerShell (PS)
(venv) > python main.py
## macOS / Linux
(venv) $ python3 main.py
### Output ###
hello world
若要幫我們的專案安裝其他套件,通常是透過指令 $ pip install <package>
如果有很多套件 (也包含不同版本) 的需求,在習慣上會新增一個稱作 requirements.txt
的檔案,儲存會用到的套件資訊,並安裝在虛擬環境中
## filename: requirememts.txt
pandas
numpy>=1.24.3
## [Windows] Command Prompt (CMD) / PowerShell (PS)
(venv) > pip install -r requirements.txt
## macOS / Linux
(venv) $ pip3 install -r requirements.txt
到這邊我們已經完成了一個基本的專案建置,是時候加入自已的程式,讓 Python 幫我們實現要完成的目標。
對於編輯器的使用,我個人推薦 Visual Studio Code ( short for: VS Code / VSC),而 IDE 的話,則是推薦 JetBrains 的 PyCharm
Visual Studio Code 在大多數情況下都能很好地勝任,同時也支援許多 Extensions,針對 ipynb
格式的 IPython Notebook 也是如此,僅需選擇正確的 Interpreter 直譯器即可執行
Keep enjoy coding with Python ! 希望這篇文章對讀者有所幫助