來自什麼是 Python?:
- Python 是一種解釋型、交互式、面向對象的程式語言。它包含了模塊、異常、動態類型、高層級動態數據類型以及類等特性。在面向對象編程以外它還支持多種編程範式,例如過程式和函數式編程等 Python 結合了超強的功能和極清晰的語法。它帶有許多系統調用和庫以及多種窗口系統的接口,並且能用 C 或 C++ 來進行擴展。它還可用作需要可編程接口的應用程式的擴展語言。最後,Python 非常易於移植:它可以在包括 Linux 和 macOS 在內的許多 Unix 變種以及 Windows 上運行。
安裝
請安裝 python包 軟體包以獲取當前版本的 Python 3。
舊版本
AUR中有之前發布的 Python 版本,可以用於運行舊程序或測試程序的版本兼容性等:
- Python 3.12: 當前版本
- Python 3.11: python311AUR
- Python 3.10: python310AUR
- Python 3.9: python39AUR
- Python 3.8: python38AUR
- Python 3.7: python37AUR
- Python 3.6: python36AUR(停止維護)
- Python 2.7: python2AUR (停止維護)
為了多版本共存,每個安裝的目錄都帶版本號,比如 Python 3.7 會安裝到 python3.7。可以通過 pyenv包 在不同版本之間切換。
在AUR上搜索python<不帶點的版本號>
可以找到對應舊版本的額外模塊和庫,例如搜索python37
可以找到3.7版本的模塊。
可以從 https://www.python.org/downloads/ 下載原始碼.
實現的替代方案
python包 包會安裝 CPython,即 Python 的參考實現。同時,還存在著多種其它的實現。這些實現通常基於舊版本的 Python,並與新版本的 CPython 不完全兼容。
Arch Linux 提供的實現包括:
- PyPy — 使用 Python 編寫的 Python 實現,較 CPython 而言具有速度和內存用量上的優勢。
- https://www.pypy.org || pypy包, pypy3包
- Jython — 使用 Java 編寫的 Python 實現。它可以用於將 Python 腳本嵌入到 Java 程序中,或是在 Python 程序中使用 Java 庫。
- micropython — 用於微控制器的 Python。它包含了 Python 標準庫的一小塊子集,並針對在微控制器或是受限環境中運行進行了優化。
- IronPython — 與 .NET 緊密整合的 Python 實現。它可以調用 .NET 庫,並可以使 .NET 程序調用 Python 庫。
Python 還有其它多種實現。有一部分實現,例如 Stackless,Pyston 和 Cinder 等已經在大型企業內部獲得使用。另外有一部分在過去較為知名的實現已經由於流行實現的改進而不再得到維護。
shell 的替代方案
python包 軟體包有一個交互式的 Python shell/REPL,可以用 python
命令啟動。也可以使用以下 shell:
- bpython — A fancy interface for the Python interpreter.
- IPython — A powerful interactive Python shell.
- Jupyter — A web-based computation application powered by IPython.
- ptpython — An advanced Python REPL built with prompt-toolkit.
軟體包管理
可以使用以下多種方式安裝Python軟體包:
- 官方軟體倉庫 和 AUR — Arch倉庫中有大量流行軟體包可用。推薦以此安裝系統級的軟體包。
- pip(1) — Python 官方的軟體包安裝器。可以使用 pip 安裝來自 Python 包索引和其他索引的軟體包。
- pipx — 與 pip 類似,但為運行它的用戶創建了一個孤立環境,用於每個應用程式及其相關軟體包,防止與系統軟體包衝突。專注於可以從命令行直接作為應用程式運行的軟體包。可以使用 pipx 安裝來自 Python Package Index 和其他索引的軟體包。
- Anaconda — 一個開源的軟體包與環境管理系統,為Python程序而創建。可以使用 Conda 安裝來自 Anaconda倉庫的軟體包。
- Miniconda — Anaconda 的輕量級替代方案,它安裝軟體包管理器,但默認不安裝科學計算軟體包。
如果使用 pip 安裝軟體包,請使用虛擬環境以避免將軟體包安裝到 /usr
。也可以使用 pip install --user
以將軟體包安裝到user scheme。pipx和 Conda 將環境管理納入了其工作流中。
若要查看 Python 軟體包管理的最佳實踐,見Python Packaging User Guide。
歷史上,easy_install(python-setuptools包的一部分)用來安裝以 Eggs 形式分發的軟體包。easy_install 和 Eggs 已經被 pip 和 Wheels 取代。詳見 pip vs easy_install 與 Wheel vs Egg。
部件綁定
以下是可用的部件工具包綁定:
- Tkinter — The standard Python interface to the Tk GUI toolkit.
- Qt for Python (PySide2) — The official Python bindings for Qt5.
- Qt for Python (PySide6) — The official Python bindings for Qt6.
- pyQt — A set of Python bindings for Qt.
- PyGObject — Python bindings for GObject based libraries such as GTK, GStreamer, WebKitGTK, GLib, and GIO.
- wxPython — A cross-platform GUI toolkit for Python which wraps wxWidgets.
若要在 Python 中使用它們,可能還需要安裝相關的部件工具包軟體包(例如,必須同時安裝 tk包 才能使用Tkinter)。
提示與技巧
虛擬環境
python-virtualenv包 是 Ian Bicking 編寫的 Python 工具,可以為 Python 建立獨立環境,可以安裝軟體包而不影響其它 virtualenv 環境或系統 Python 軟體包,可以修改一個軟體使用的 Python 版本。
Python 提供了創建隔離的虛擬環境的工具,可以將軟體包安裝到其中,而不會與其他虛擬環境或系統軟體包發生衝突。虛擬環境也可以在同一個系統上運行不同版本的 Python。
詳見 Python/Virtual environment。
在 Python Shell 中實現 Tab 補全功能
Tab completion is available in the interactive shell by default. Note that the readline completer will only complete names in the global namespace. You can use python-jedi包 for a richer tab completion experience [1].
另見
官方
第三方
- Automate the Boring Stuff with Python - Creative Commons book
- Awesome Python - A curated list of Python resources
- A Byte of Python - Creative Commons book
- Cracking Codes With Python - Free online book
- Crash into Python - Free tutorial
-
Debugging in Python - Guide to using
pdb
, the Python debugger - Dive Into Python - Creative Commons book
- Fluent Python - Commercial book
- Introducing Python - Commercial book
- Invent Your Own Computer Games with Python - Free online book
- Learn Python - Free interactive tutorial
- Learn Python the Hard Way - Commercial book
- Pythonspot Python Tutorials - Free online tutorials
- Think Python - Creative Commons book