Python
来自什么是 Python?:
- Python 是一种解释型、交互式、面向对象的编程语言。它包含了模块、异常、动态类型、高层级动态数据类型以及类等特性。在面向对象编程以外它还支持多种编程范式,例如过程式和函数式编程等 Python 结合了超强的功能和极清晰的语法。它带有许多系统调用和库以及多种窗口系统的接口,并且能用 C 或 C++ 来进行扩展。它还可用作需要可编程接口的应用程序的扩展语言。最后,Python 非常易于移植:它可以在包括 Linux 和 macOS 在内的许多 Unix 变种以及 Windows 上运行。
安装[编辑 | 编辑源代码]
请安装 python包 软件包以获取当前版本的 Python 3。
旧版本[编辑 | 编辑源代码]
AUR中有之前发布的 Python 版本,可以用于运行旧程序或测试程序的版本兼容性等:
- Python 3.11: python311AUR
- Python 3.10: Current release, see previous section
- 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