2025年11月9日 星期日

樹莓派 Pi 400 安裝 Python 套件

今天要來檢視 Pi 400 的運行環境, 安裝所需的 Python 套件. Raspbian OS Bookworm (源自 Debian 12) 內建的 Python 環境是 v3.11.2 :

pi@raspberrypi:~ $ python 
Python 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.


1. 系統層級的 Python 環境 : 

用 pip list 查詢發現, Bookworm 的 Python 11.2 已預裝 Pillow, numpy, bs4 與 Flask 等第三方套件, 這些套件都是 Debian 預設之系統層級套件 :

pi@raspberrypi:~ $ pip list   
Package                            Version
---------------------------------- ----------
asgiref                            3.6.0
astroid                            2.14.2
asttokens                          2.2.1
attrs                              22.2.0
av                                 12.3.0
Babel                              2.10.3
beautifulsoup4                     4.11.2
blinker                            1.5
certifi                            2022.9.24
chardet                            5.1.0
charset-normalizer                 3.0.1
click                              8.1.3
colorama                           0.4.6
colorzero                          2.0
cryptography                       38.0.4
cupshelpers                        1.0
dbus-python                        1.3.2
dill                               0.3.6
distro                             1.8.0
docutils                           0.19
Flask                              2.2.2
gpiozero                           2.0.1
html5lib                           1.1
idna                               3.3
importlib-metadata                 4.12.0
isort                              5.6.4
itsdangerous                       2.1.2
jedi                               0.18.2
Jinja2                             3.1.2
jsonpointer                        2.3
jsonschema                         4.10.3
lazy-object-proxy                  1.9.0
lgpio                              0.2.2.0
libarchive-c                       2.9
libevdev                           0.5
logilab-common                     1.9.8
lxml                               4.9.2
MarkupSafe                         2.1.2
mccabe                             0.7.0
meson                              1.5.1
more-itertools                     8.10.0
mypy                               1.0.1
mypy-extensions                    0.4.3
numpy                              1.24.2
oauthlib                           3.2.2
olefile                            0.46
parso                              0.8.3
pexpect                            4.8.0
pgzero                             1.2
picamera2                          0.3.31
pidng                              4.0.9
piexif                             1.1.3
pigpio                             1.78
Pillow                             9.4.0
pip                                23.0.1
platformdirs                       2.6.0
psutil                             5.9.4
ptyprocess                         0.7.0
pycairo                            1.20.1
pycryptodomex                      3.11.0
pycups                             2.0.1
pygame                             2.1.2
Pygments                           2.14.0
PyGObject                          3.42.2
pyinotify                          0.9.6
PyJWT                              2.6.0
pylint                             2.16.2
PyOpenGL                           3.1.6
pyOpenSSL                          23.0.0
PyQt5                              5.15.9
PyQt5-sip                          12.11.1
pyrsistent                         0.18.1
pyserial                           3.5
pysmbc                             1.0.23
python-apt                         2.6.0
python-dotenv                      0.21.0
python-prctl                       1.8.1
pytz                               2022.7.1
pyudev                             0.24.0
reportlab                          3.6.12
requests                           2.28.1
requests-oauthlib                  1.3.0
responses                          0.18.0
rfc3987                            1.3.8
roman                              3.3
rpi-keyboard-config                1.0
rpi-lgpio                          0.6
RTIMULib                           7.2.1
Send2Trash                         1.8.1b0
sense-hat                          2.6.0
setuptools                         66.1.1
simplejpeg                         1.8.1
simplejson                         3.18.3
six                                1.16.0
smbus2                             0.4.2
soupsieve                          2.3.2
spidev                             3.5
ssh-import-id                      5.10
thonny                             4.1.4
toml                               0.10.2
tomlkit                            0.11.7
tqdm                               4.64.1
twython                            3.8.2
types-aiofiles                     22.1
types-annoy                        1.17
types-appdirs                      1.4
types-aws-xray-sdk                 2.10
types-babel                        2.11
types-backports.ssl-match-hostname 3.7
types-beautifulsoup4               4.11
types-bleach                       5.0
types-boto                         2.49
types-braintree                    4.17
types-cachetools                   5.2
types-caldav                       0.10
types-certifi                      2021.10.8
types-cffi                         1.15
types-chardet                      5.0
types-chevron                      0.14
types-click-spinner                0.1
types-colorama                     0.4
types-commonmark                   0.9
types-console-menu                 0.7
types-contextvars                  2.4
types-croniter                     1.3
types-cryptography                 3.3
types-D3DShot                      0.1
types-dateparser                   1.1
types-DateTimeRange                1.2
types-decorator                    5.1
types-Deprecated                   1.2
types-dj-database-url              1.0
types-docopt                       0.6
types-docutils                     0.19
types-editdistance                 0.6
types-emoji                        2.1
types-entrypoints                  0.4
types-first                        2.0
types-flake8-2020                  1.7
types-flake8-bugbear               22.10.27
types-flake8-builtins              2.0
types-flake8-docstrings            1.6
types-flake8-plugin-utils          1.3
types-flake8-rst-docstrings        0.2
types-flake8-simplify              0.19
types-flake8-typing-imports        1.14
types-Flask-Cors                   3.0
types-Flask-SQLAlchemy             2.5
types-fpdf2                        2.5
types-gdb                          12.1
types-google-cloud-ndb             1.11
types-hdbcli                       2.14
types-html5lib                     1.1
types-httplib2                     0.21
types-humanfriendly                10.0
types-invoke                       1.7
types-JACK-Client                  0.5
types-jmespath                     1.0
types-jsonschema                   4.17
types-keyboard                     0.13
types-ldap3                        2.9
types-Markdown                     3.4
types-mock                         4.0
types-mypy-extensions              0.4
types-mysqlclient                  2.1
types-oauthlib                     3.2
types-openpyxl                     3.0
types-opentracing                  2.4
types-paho-mqtt                    1.6
types-paramiko                     2.11
types-parsimonious                 0.10
types-passlib                      1.7
types-passpy                       1.0
types-peewee                       3.15
types-pep8-naming                  0.13
types-Pillow                       9.3
types-playsound                    1.3
types-polib                        1.1
types-prettytable                  3.4
types-protobuf                     3.20
types-psutil                       5.9
types-psycopg2                     2.9
types-pyaudio                      0.2
types-PyAutoGUI                    0.9
types-pycurl                       7.45
types-pyfarmhash                   0.3
types-pyflakes                     2.5
types-Pygments                     2.13
types-pyinstaller                  5.6
types-PyMySQL                      1.0
types-pynput                       1.7
types-pyOpenSSL                    22.1
types-pyRFC3339                    1.1
types-PyScreeze                    0.1
types-pysftp                       0.2
types-pytest-lazy-fixture          0.6
types-python-crontab               2.6
types-python-dateutil              2.8
types-python-gflags                3.1
types-python-jose                  3.3
types-python-nmap                  0.7
types-python-slugify               6.1
types-pytz                         2022.6
types-pyvmomi                      7.0
types-pywin32                      304
types-PyYAML                       6.0
types-redis                        4.3
types-regex                        2022.10.31
types-requests                     2.28
types-retry                        0.9
types-Send2Trash                   1.8
types-setuptools                   65.5
types-simplejson                   3.17
types-singledispatch               3.7
types-six                          1.16
types-slumber                      0.7
types-SQLAlchemy                   1.4.43
types-stdlib-list                  0.8
types-stripe                       3.5
types-tabulate                     0.9
types-termcolor                    1.1
types-toml                         0.10
types-toposort                     1.7
types-tqdm                         4.64
types-tree-sitter                  0.20
types-tree-sitter-languages        1.5
types-ttkthemes                    3.2
types-typed-ast                    1.5
types-tzlocal                      4.2
types-ujson                        5.5
types-urllib3                      1.26
types-vobject                      0.9
types-waitress                     2.1
types-whatthepatch                 1.0
types-xmltodict                    0.13
types-xxhash                       3.0
types-zxcvbn                       4.4
typing_extensions                  4.4.0
uritemplate                        4.1.1
urllib3                            1.26.12
videodev2                          0.0.4
webcolors                          1.11.1
webencodings                       0.5.1
Werkzeug                           2.2.2
wheel                              0.38.4
wrapt                              1.14.1
zipp                               1.0.0


2. pip 與 pip3 : 

以前舊版的 Raspbian OS (例如 Buster 之前) 上同時存在 Python 2 與 Python 3. 在安裝套件時要用 pip3 install 而非 pip install. 但是從 Bookworm 版開始 Python 2 已退場, 所以用 pip 或 pip3 是一樣的. 用 which 指令查詢 pip 與 pip3, 會發現雖然系統中仍有兩個名稱不同的指令檔 :

pi@raspberrypi:~ $ which pip   
/usr/bin/pip
pi@raspberrypi:~ $ which pip3   
/usr/bin/pip3

但用 --version 查詢這兩個指令檔版本, 它們實際上指向同一個 Python 3 版本 :

pi@raspberrypi:~ $ pip --version   
pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)
pi@raspberrypi:~ $ pip3 --version   
pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)

這兩個指令檔都是 symlink, 最終都執行 /usr/bin/python3 -m pip 指令. 


3. PEP 668 的限制 : 

Raspberry Pi OS 自 Bookworm 版開始強制啟用了 PEP 668 的限制, 不允許直接在系統的 Python 3 環境 (/usr/lib/python3/) 中安裝套件, 以免使用者用 pip 覆蓋掉用 apt 安裝的 Python 系統套件, 或者出現版本衝突問題. 

上面用 pip list 列出的是 Debian 系統層級的套件 (放在 /usr/lib/python3/dist-packages), 直接用 pip install 安裝新套件會被 PEP 668 阻止而出現 "外部管理環境" 錯誤, 例如 :

pi@raspberrypi:~ $ pip install html5lib  
error: externally-managed-environment 

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    For more information visit http://rptl.io/venv

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

從說明中可知, 若要在系統層級安裝套件 xyz, 必須使用 apt install python3-xyz 指令才行 (apt 是 Debian 的套件管理系統). 但官方並不鼓勵這麼做, 因為這可能會無意中覆蓋掉系統套件 (例如升級 Flask 後使系統工具損壞無法使用), 而且混用 apt/pip 易導致依賴衝突. 所有系統層級的 Python 套件都應由 APT (Debian 套件管理系統) 負責安裝.

Debian 官方建議一般專案開發使用 venv 建立虛擬環境, 指令如下 : 

python -m venv 虛擬環境資料夾     (建立虛擬環境)
source ~/虛擬環境資料夾/bin/activate    (啟用虛擬環境) 

可以視專案需求建立許多不同的虛擬環境, 分別安裝不同版本的 Python 套件, 這些虛擬環境彼此獨立互相隔離, 不會有彼此版本衝突問題, 也不會影響到用 apt 安裝的系統層級 Python 套件. 

若系統環境與虛擬環境同時有安裝某套件 (例如 Numpy), 則用虛擬環境執行有匯入使用該套件之 Python 程式時, 會優先使用該環境中所安裝的套件, 如果沒有安裝此套件就會自動退回 (fallback) 使用系統層級套件. 

例如產生一個 myenv 資料夾來建立虛擬環境 : 

pi@raspberrypi:~ $ python -m venv myenv  
pi@raspberrypi:~ $ ls -ls myenv   
總用量 16
4 drwxr-xr-x 2 pi pi 4096 11月  8 09:12 bin
4 drwxr-xr-x 3 pi pi 4096 11月  8 09:11 include
4 drwxr-xr-x 3 pi pi 4096 11月  8 09:11 lib
0 lrwxrwxrwx 1 pi pi    3 11月  8 09:11 lib64 -> lib
4 -rw-r--r-- 1 pi pi  152 11月  8 09:11 pyvenv.cfg

python -m venv 指令會在虛擬環境資料夾下的 bin 子資料夾建立符號連結 (symlink) 指向系統的 Python 可執行檔與標準函式庫, 在虛擬環境中用 pip install 安裝的第三方套件則是放在 lib 子資料夾下. 

啟用虛擬環境要用 source 指令在目前 shell 中執行 myenv/bin 下的 activate 腳本 : 

pi@raspberrypi:~ $ source ~/myenv/bin/activate   
(myenv) pi@raspberrypi:~ $ 

前面有 (myenv) 表示目前已進入名為 myenv 的虛擬環境中, 然後就可以在此虛擬環境中用 pip install 安裝所需要的套件. 注意, 虛擬環境只是隔離 Python 執行環境而已, 主要是 python, pip 和套件安裝目錄, 使不同專案可使用不同版本套件, 不會影響系統 Python 或其他專案. 

在虛擬環境中可以存取到檔案系統與使用者的家目錄, 虛擬環境不會封鎖對系統資源的存取, 與在系統環境一樣可以使用網路, 磁碟, 其他程式, 與環境變數, 只是 Python 的套件路徑和執行檔會指向 venv 而不是系統 Python, 亦即, venv 只是Python 的虛擬沙箱, 不會把整個使用者環境封鎖起來. 

(myenv) pi@raspberrypi:~ $ ls -ls  
總用量 40
4 drwxr-xr-x 2 pi pi 4096 10月  1 08:10 Bookshelf
4 drwxr-xr-x 2 pi pi 4096 10月  1 08:23 Desktop
4 drwxr-xr-x 2 pi pi 4096 10月  1 08:23 Documents
4 drwxr-xr-x 2 pi pi 4096 11月  6 12:50 Downloads
4 drwxr-xr-x 2 pi pi 4096 10月  1 08:23 Music
4 drwxr-xr-x 5 pi pi 4096 11月  8 14:48 myenv
4 drwxr-xr-x 2 pi pi 4096 10月  1 08:23 Pictures
4 drwxr-xr-x 2 pi pi 4096 10月  1 08:23 Public
4 drwxr-xr-x 2 pi pi 4096 10月  1 08:23 Templates
4 drwxr-xr-x 3 pi pi 4096 11月  6 22:31 Videos

跳出虛擬環境的指令為 deactivate : 

(myenv) pi@raspberrypi:~ $ deactivate   
pi@raspberrypi:~ $ 

這樣就跳出虛擬環境, 回到系統環境. 由於虛擬環境都放在指定資料夾下, 若要移除虛擬環境只要將該資料夾刪除即可. 


4. 在虛擬環境安裝 Python 套件 : 

此 Pi 400 主機主要是作為爬蟲與網頁伺服器之用, 所以 requests, bs4, selenium, scrapy, flask 與 django 都是必要配備. 但從上面 pip list 的結果可知, requests 與 flask 在系統 Python 環境中已預先安裝, 在虛擬環境中毋須再安裝, 除非專案中要用到的套件版本與系統 Python 中的既有套件不同才需要安裝. 

我參考前陣子樹莓派 Pi 3 A+ 的安裝項目, 在虛擬環境中安裝所需套件 : 


例如 :

(myenv) pi@raspberrypi:~ $ pip install html5lib  
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting html5lib
  Downloading https://www.piwheels.org/simple/html5lib/html5lib-1.1-py2.py3-none-any.whl (117 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 117.4/117.4 kB 203.4 kB/s eta 0:00:00
Collecting six>=1.9
  Downloading https://www.piwheels.org/simple/six/six-1.17.0-py2.py3-none-any.whl (11 kB)
Collecting webencodings
  Downloading https://www.piwheels.org/simple/webencodings/webencodings-0.5.1-py2.py3-none-any.whl (11 kB)
Installing collected packages: webencodings, six, html5lib
Successfully installed html5lib-1.1 six-1.17.0 webencodings-0.5.1
(myenv) pi@raspberrypi:~ $ pip show html5lib
Name: html5lib
Version: 1.1
Summary: HTML parser based on the WHATWG HTML specification
Home-page: https://github.com/html5lib/html5lib-python
Author: 
Author-email: 
License: MIT License
Location: /home/pi/myenv/lib/python3.11/site-packages
Requires: six, webencodings
Required-by: 


(1). 爬蟲套件 :

pip install html5lib
pip install selenium
pip install scrapy 


(2). 架站 & Web UI 套件 :  

pip install django
pip install streamlit  
pip install gradio  


(3). 量化投資套件 : 

pip install yfinance 
pip install twstock
pip install mplfinance 
pip install ta
pip install kbar 
pip install backtrader
pip install pyfolio 
pip install pyfolio_reloaded (pyfolio 的後續維護版) 
編譯 Ta-Lib 套件


(4). 資料科學 & 機器學習套件 : 

pip install pandas (安裝 stremlit 時已安裝)
pip install scipy (安裝 pyfolio 時已安裝)
pip install matplotlib (安裝 kbar 時已安裝)
pip install seaborn (安裝 pyfolio 時已安裝)
pip install bokeh 
pip install plotly
pip install altair (安裝 stremlit 時已安裝)
pip install scikit-learn (安裝 pyfolio 時已安裝)


(5). Bot 套件 : 

pip install line-bot-sdk
pip install python-telegram-bot 


(6). LLM 套件 : 

pip install openai 
pip install google-generativeai

注意, kbar 套件因為 mplfinance 關係要求 numpy < 2, 但 gradio, yfinance 卻安裝 numpy > 2, 兩者可能有衝突 (但也許還是可跑, 要測試確認). 

在虛擬環境中下 pip list 指令會顯示此虛擬環境已安裝的第三方套件, 新建立的虛擬環境用 pip list 檢視只有 pip 與 setuptools 這兩個套件而已 :

(myenv) pi@raspberrypi:~ $ pip list  
Package    Version
---------- -------
pip        23.0.1
setuptools 66.1.1

下面是安裝 htmllib 與 selenium 套件後的 pip list 結果 (其他為相依模組或套件) :

(myenv) pi@raspberrypi:~ $ pip list   
Package           Version
----------------- -----------
attrs             25.4.0
certifi           2025.10.5
h11               0.16.0
html5lib          1.1
idna              3.11
outcome           1.3.0.post0
pip               23.0.1
PySocks           1.7.1
selenium          4.38.0
setuptools        66.1.1
six               1.17.0
sniffio           1.3.1
sortedcontainers  2.4.0
trio              0.32.0
trio-websocket    0.12.2
typing_extensions 4.15.0
urllib3           2.5.0
webencodings      0.5.1
websocket-client  1.9.0
wsproto           1.2.0

每一個虛擬環境內的 pip list 結果會因為用 pip install 安裝了不同套件, 或不同版本的相同套件而有差異, 從而避免了版本衝突問題. 

下面是安裝套件前的 df -h 結果 : 

pi@raspberrypi:~ $ df -h  
檔案系統        容量  已用  可用 已用% 掛載點
udev            1.6G     0  1.6G    0% /dev
tmpfs           760M   39M  722M    6% /run
/dev/mmcblk0p2   59G  5.5G   50G   10% /
tmpfs           1.9G  892K  1.9G    1% /dev/shm
tmpfs           5.0M   12K  5.0M    1% /run/lock
/dev/mmcblk0p1  510M   78M  433M   16% /boot/firmware
tmpfs           380M   44K  380M    1% /run/user/1000

pi@raspberrypi:~ $ lsblk  
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
mmcblk0     179:0    0 59.5G  0 disk 
├─mmcblk0p1 179:1    0  512M  0 part /boot/firmware
└─mmcblk0p2 179:2    0   59G  0 part /

安裝完上面全部套件後用 pip list 檢視 myenv 虛擬目錄的 Python 執行環境 : 

(myenv) pi@raspberrypi:~ $ pip list 
Package                      Version
---------------------------- -----------
aenum                        3.1.16
aiofiles                     24.1.0
aiohappyeyeballs             2.6.1
aiohttp                      3.13.2
aiosignal                    1.4.0
altair                       5.5.0
annotated-doc                0.0.3
annotated-types              0.7.0
anyio                        4.11.0
asgiref                      3.10.0
asttokens                    3.0.0
attrs                        25.4.0
Automat                      25.4.16
backtrader                   1.9.78.123
beautifulsoup4               4.14.2
blinker                      1.9.0
bokeh                        3.8.1
Bottleneck                   1.6.0
brotli                       1.2.0
cachetools                   6.2.1
certifi                      2025.10.5
cffi                         2.0.0
charset-normalizer           3.4.4
click                        8.3.0
constantly                   23.10.4
contourpy                    1.3.3
cryptography                 46.0.3
cssselect                    1.3.0
curl_cffi                    0.13.0
cycler                       0.12.1
decorator                    5.2.1
defusedxml                   0.7.1
Deprecated                   1.3.1
distro                       1.9.0
Django                       5.2.8
empyrical                    0.5.5
empyrical-reloaded           0.5.12
executing                    2.2.1
fastapi                      0.121.1
ffmpy                        0.6.4
filelock                     3.20.0
fonttools                    4.60.1
frozendict                   2.4.6
frozenlist                   1.8.0
fsspec                       2025.10.0
future                       1.0.0
gitdb                        4.0.12
GitPython                    3.1.45
google-ai-generativelanguage 0.6.15
google-api-core              2.28.1
google-api-python-client     2.187.0
google-auth                  2.43.0
google-auth-httplib2         0.2.1
google-generativeai          0.8.5
googleapis-common-protos     1.72.0
gradio                       5.49.1
gradio_client                1.13.3
groovy                       0.1.2
grpcio                       1.76.0
grpcio-status                1.71.2
h11                          0.16.0
hf-xet                       1.2.0
html5lib                     1.1
httpcore                     1.0.9
httplib2                     0.31.0
httpx                        0.28.1
huggingface_hub              1.1.2
hyperlink                    21.0.0
idna                         3.11
incremental                  24.7.2
ipython                      9.7.0
ipython_pygments_lexers      1.1.1
itemadapter                  0.12.2
itemloaders                  1.3.2
jedi                         0.19.2
Jinja2                       3.1.6
jiter                        0.11.1
jmespath                     1.0.1
joblib                       1.5.2
jsonschema                   4.25.1
jsonschema-specifications    2025.9.1
kbar                         0.1.2
kiwisolver                   1.4.9
line-bot-sdk                 3.21.0
lxml                         6.0.2
markdown-it-py               4.0.0
MarkupSafe                   3.0.3
matplotlib                   3.10.7
matplotlib-inline            0.2.1
mdurl                        0.1.2
mplfinance                   0.12.10b0
multidict                    6.7.0
multitasking                 0.0.12
narwhals                     2.10.2
numpy                        1.26.4
openai                       2.7.1
orjson                       3.11.4
outcome                      1.3.0.post0
packaging                    25.0
pandas                       2.3.3
pandas-datareader            0.10.0
parsel                       1.10.0
parso                        0.8.5
peewee                       3.17.3
pexpect                      4.9.0
pillow                       11.3.0
pip                          23.0.1
platformdirs                 4.5.0
plotly                       6.4.0
prompt_toolkit               3.0.52
propcache                    0.4.1
Protego                      0.5.0
proto-plus                   1.26.1
protobuf                     5.29.5
ptyprocess                   0.7.0
pure_eval                    0.2.3
pyarrow                      21.0.0
pyasn1                       0.6.1
pyasn1_modules               0.4.2
pycparser                    2.23
pydantic                     2.11.10
pydantic_core                2.33.2
pydeck                       0.9.1
PyDispatcher                 2.0.7
pydub                        0.25.1
pyfolio                      0.9.2
pyfolio-reloaded             0.9.9
Pygments                     2.19.2
pyOpenSSL                    25.3.0
pyparsing                    3.2.5
PySocks                      1.7.1
python-dateutil              2.9.0.post0
python-multipart             0.0.20
python-telegram-bot          22.5
pytz                         2025.2
PyYAML                       6.0.3
queuelib                     1.8.0
referencing                  0.37.0
requests                     2.32.5
requests-file                3.0.1
rich                         14.2.0
rpds-py                      0.28.0
rsa                          4.9.1
ruff                         0.14.4
safehttpx                    0.1.7
scikit-learn                 1.7.2
scipy                        1.16.3
Scrapy                       2.13.3
seaborn                      0.13.2
selenium                     4.38.0
semantic-version             2.10.0
service-identity             24.2.0
setuptools                   66.1.1
shellingham                  1.5.4
six                          1.17.0
smmap                        5.0.2
sniffio                      1.3.1
sortedcontainers             2.4.0
soupsieve                    2.8
sqlparse                     0.5.3
stack-data                   0.6.3
starlette                    0.49.3
streamlit                    1.51.0
ta                           0.11.0
tenacity                     9.1.2
threadpoolctl                3.6.0
tldextract                   5.3.0
toml                         0.10.2
tomlkit                      0.13.3
tornado                      6.5.2
tqdm                         4.67.1
traitlets                    5.14.3
trio                         0.32.0
trio-websocket               0.12.2
Twisted                      25.5.0
twstock                      1.4.0
typer                        0.20.0
typer-slim                   0.20.0
typing_extensions            4.15.0
typing-inspection            0.4.2
tzdata                       2025.2
uritemplate                  4.2.0
urllib3                      2.5.0
uvicorn                      0.38.0
w3lib                        2.3.1
watchdog                     6.0.0
wcwidth                      0.2.14
webencodings                 0.5.1
websocket-client             1.9.0
websockets                   15.0.1
wrapt                        2.0.1
wsproto                      1.2.0
xyzservices                  2025.10.0
yarl                         1.22.0
yfinance                     0.2.66
zope.interface               8.0.1

用 df -h 檢視 TF 卡 :

(myenv) pi@raspberrypi:~ $ df -h  
檔案系統        容量  已用  可用 已用% 掛載點
udev            1.6G     0  1.6G    0% /dev
tmpfs           760M   39M  722M    6% /run
/dev/mmcblk0p2   59G  7.2G   48G   14% /
tmpfs           1.9G  892K  1.9G    1% /dev/shm
tmpfs           5.0M   12K  5.0M    1% /run/lock
/dev/mmcblk0p1  510M   78M  433M   16% /boot/firmware
tmpfs           380M   44K  380M    1% /run/user/1000

安裝上面這些套件用掉約 1.7GB 容量. 


5. 在 crontab 中用虛擬環境執行 Python 程式 : 

要定時或週期性執行 Python 程式首先須用 chmod 指令將其權限設為可執行, 例如 :

chmod +x myscript.py   

然後在 crontab 中指定一個 Python 虛擬環境來執行該程式, 例如 : 

0 6,16 * * * /home/pi/myenv/bin/python /home/pi/myprojects/myscript.py   

但這個方法只能執行一個 Python 程式, 如果要同時執行多個程式, 可以先寫一個 bash wrapper 程式, 例如 : 

# run_myscript.sh
# 啟動虛擬環境
source /home/pi/myenv/bin/activate
# 執行 Python 腳本
python /home/pi/myprojects/myscript_1.py
python /home/pi/myprojects/myscript_2.py

crontab 可以這樣寫 :

0 6,16 * * * /home/pi/run_myscript.sh


2025-11-10 補充 : 

漏了一個套件 jupyterlab, 因 pyfolio 需要使用它的網頁介面輸出結果 :

pip install jupyterlab 

此套件頗大, 吃掉約 200MB (已用從 7.2GB -> 7.4GB) :

(myenv) pi@raspberrypi:~ $ df -h  
檔案系統        容量  已用  可用 已用% 掛載點
udev            1.6G     0  1.6G    0% /dev
tmpfs           760M   47M  713M    7% /run
/dev/mmcblk0p2   59G  7.4G   48G   14% /
tmpfs           1.9G  892K  1.9G    1% /dev/shm
tmpfs           5.0M   12K  5.0M    1% /run/lock
/dev/mmcblk0p1  510M   78M  433M   16% /boot/firmware
tmpfs           380M   44K  380M    1% /run/user/1000


2025-11-20 補充 :

我已於這兩天改換 TF 卡, 燒錄最新的樹莓派 Trixie 作業系統, 但它內建的 Python 3.13 太新, 有些套件不支援, 所以我又編譯安裝了有普遍支援的 Python 3.10 來用, 參考 : 


沒有留言 :