建置 Python 3.9 開發環境

六小編
Join to follow...
Follow/Unfollow Writer: 六小編
By following, you’ll receive notifications when this author publishes new articles.
Don't wait! Sign up to follow this writer.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.
六小編就是六小編,六小編不是七小編。
263   0  
·
2020/12/20
·
5 mins read


Python 的版本

Python 進入 3.x 的時代也好幾年了,但至今 Python 2.7.x 即便已經不再維護,它還是以某種殭屍的型態存活在各個陳年專案上,對一個沒有舊包袱的新專案來說,Python 3.x 的 x 就必須是在開立專案時要考慮的問題,在 Python 的網站上有 Python 各版本目前的生命週期表

Python version Maintenance status First released End of support
3.9 bugfix 2020-10-05 2025-10
3.8 bugfix 2019-10-14 2024-10
3.7 security 2018-06-27 2023-06-27
3.6 security 2016-12-23 2021-12-23
2.7 end-of-life 2010-07-03 2020-01-01

可以看到從 Python 3.7 起就維持一年跳一個次版號的頻率,並維持著五年的生命週期。

在確認過 Python 本身的生命週期後,另外一個考慮的點是 Python 3.6 ~ 3.9 之間是否有淘汰某些舊的語法、標準庫、功能,如果有的話那必須進一步考慮到新專案的依賴套件是不是有用到那些被淘汰的功能,幸好 Python 3.6 ~ 3.9 主要都是增加新的功能或語法,不太有淘汰舊功能的問題,因此對於新成立的專案,採用較新的 Python 3.9 看起來是個不太有風險的決定。

補充一個消息,從 Python 3.9 的下一版起會淘汰設計給保留與 Python 2.7 相容的舊標準庫/函式/API

Python 3.9 開發環境

目前所有的 Linux 發行版,內建的 Python 版本各有不同,如果您的 Linux 較新,已經是內建 Python 3.9 的話那恭喜您本文對您是無用處的,本文完。

以常見的 Linux 發行版來說,內建的 Python 如下:

Linux 發行版 Python 版次
Ubuntu 20.04 LTS Python 3.8.2
Linux Mint 20 Python 3.8.2
Pop!_OS 20.04 Python 3.8.2
Debian 10 Python 3.7.3
Elementary OS 6 Odin Python 3.8.10
CentOS 8.2.2004 Python 3.8.0

上面的列表涵蓋了主要的桌面端與伺服器端的 Linux 發行版,可以看到今年十月才發布的 Python 3.9 理所當然地尚未被納入。

如果是 macOS Catalina,內建的 Python 版本分別是 2.7.16 和 3.8.6。

下面開始我們的 Python 3.9 開發環境建置之旅。

多版本 Python 管理

想要把 Python 3.9 裝起來,當然不能簡單粗暴地把作業系統預載的 Python 取代掉,這樣做會造成後續系統維護與更新上的許多問題。在這邊我們利用 pyenv 這個多版本 Python 管理器來幫助我們安裝一個與系統預載 Python 並行存在的 Python 3.9。

pyenv 不僅能幫我們安裝多版本的 Python,也提供了 Python 版本切換與管理的能力,並且這些由 pyenv 管理的 Python 版本彼此間都互相獨立,包括 Python 之下的套件/模組也都是獨立的,這樣的好處就是讓一台電腦的開發環境可以既多元又單純,多元的是 Python 版本,讓新舊專案都可以有適用的 Python 版本;單純的是各版次 Python 下的套件與模組,並不會受到別的版次的 Python 所影響。

根據 pyenv 的要求,需要安裝編譯 Python 所必須的其它系統套件,因為 pyenv 安裝 Python 的機制是把指定版本的 Python 源碼抓下來在自己的電腦內編譯,因此必須安裝那些編譯用的系統套件,才可以順利的編譯出 Python。

對 Debian / Elementary OS / Pop!_OS / Linux Mint / Ubuntu 等同一脈的 Linux 發行版,需要安裝下列套件:

sudo apt install --no-install-reocmmends \
make \
build-essential \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
wget \
curl \
llvm \
libncurses5-dev \
libncursesw5-dev \
xz-utils \
tk-dev \
libffi-dev \
liblzma-dev \
libxml2-dev \
libxmlsec1-dev

如果是其它 Linux 發行版或其它作業系統,請參考 pyenv 的文件「Suggested build environment」一節安裝所需系統套件。(像這樣的套件依賴關係就像粽子串一樣,一個牽一個,全部拉起來就是一大堆⋯⋯。)

上面的系統套件裝完後,終於可以裝 pyenv 了,Linux 安裝 pyenv 有現成的安裝腳本可以使用:

> curl https://pyenv.run | bash

如果是 macOS,則是利用 Homebrew 安裝 pyenv:

> brew install pyenv

然後把 pyenv 加入 shell 的設定檔內,讓我們進入 shell 時就先執行 pyenv init 的命令:

> echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc

以及設定讓編譯 Python 時取用 Homebrew 版的 ziplib 和 bzip2 函式庫:

> echo 'export PATH="/usr/local/opt/bzip2/bin:$PATH"' >> ~/.zshrc
> export LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/bzip2/lib"
> export CPPFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/bzip3/include"

上面是以 Zsh 為例,如果是其他 shell 要自行類推修改,完整的 pyenv 安裝流程和注意事項最好還是看 pyenv 的文件

安裝完之後,如果 pyenv 有被正確的加進 $PATH 內,那麼執行 pyenv 應該可以看到如下的說明畫面:

pyenv 1.2.21
Usage: pyenv <command> [<args>]Some useful pyenv commands are:
   activate             Activate virtual environment
   commands             List all available pyenv commands
   deactivate           Deactivate virtual environment
   doctor               Verify pyenv installation and development tools to build pythons.
   exec                 Run an executable with the selected Python version
   global               Set or show the global Python version(s)
   help                 Display help for a command
   hooks                List hook scripts for a given pyenv command
   init                 Configure the shell environment for pyenv
   install              Install a Python version using python-build
   local                Set or show the local application-specific Python version(s)
   prefix               Display prefix for a Python version
   rehash               Rehash pyenv shims (run this after installing executables)
   root                 Display the root directory where versions and shims are kept
   shell                Set or show the shell-specific Python version
   shims                List existing pyenv shims
   uninstall            Uninstall a specific Python version
   version              Show the current Python version(s) and its origin
   --version            Display the version of pyenv
   version-file         Detect the file that sets the current pyenv version
   version-name         Show the current Python version
   version-origin       Explain how the current Python version is set
   versions             List all Python versions available to pyenv
   virtualenv           Create a Python virtualenv using the pyenv-virtualenv plugin
   virtualenv-delete    Uninstall a specific Python virtualenv
   virtualenv-init      Configure the shell environment for pyenv-virtualenv
   virtualenv-prefix    Display real_prefix for a Python virtualenv version
   virtualenvs          List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
   whence               List all Python versions that contain the given executable
   which                Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.


Article info

Categories:
Date:
Published: 2020/12/20 - Updated: 2021/10/18
Total: 1429 words


Share this article:
About the Author

六小編就是六小編,六小編不是七小編。




Join the discussion now!
Don't wait! Sign up to join the discussion.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.