Python解释器的几种实现版本

在选择Python语言进行软件开发时,我们面对的实际是一套大型语言生态系统——其中包含各类涵盖无数编程需求的软件包。但除了从GUI开发到机器学习的各类库之外,各位还能够从多种Python运行时中作出选择——其中部分运行时也许更适合您当前的用例条件。

下面,我们将简单介绍目前最常用的几款Python发行版——从标准实现(CPython)到针对速度进行优化的版本(PyPy),再到特殊用例(Anaconda、ActivePython)乃至最初为完全不同的其他语言设计的运行时(Jython、IronPython)。

目录

CPython

Anaconda Python

ActivePython

PyPy

Jython

一、CPython

CPython属于Python参考实现方案,可算是所有其他Python衍生发行版的一套标准化版本。CPython利用C语言编写而成,而其编写者包含多位Python语言顶级决策层中的核心人员。

1.CPython用例

CPython属于Python的参考实现版本,因为其在优化方面表现得最为保守。当然,这并不是缺点,而是设计取向。Python的维护者们希望将CPython打造为Python最具广泛兼容性与标准化的实现方案。

CPython最适合对Python标准的兼容性与一致性要求较高的用户。此外,CPython同样适用于希望以最基本方式使用Python并愿意为此放弃某些便捷性的专业人士。

举例来说,您需要进行些许调整才能利用CPython设置虚拟环境。而其他发行版(Anaconda)则在工作区设置中提供更多自动化功能。

2.CPython的局限性

CPython并不像Python的其他版本那样对性能作出深度优化。其不提供原生JIT(即时)编译器,不提供加速数学库,也没有用于提升性能的第三方附加选项。

您当然可以根据需求自行添加,但其并不直接提供绑定包。当然,这一切都是设计取向所决定,旨在确保CPython作为一套参考实现版本能够最大程度实现兼容性与标准化。而性能优化——开发人员可以选择其他配合工具包进行添加。

此外,CPython仅提供一组用于使用Python的基准性工具。举例来说,pip软件包管理器会从Python原生PyPI软件包库当中获取并安装各软件包。如果开发者允许,Pip甚至可以安装经过预编译的二进制文件(通过轮盘发布格式),但无法安装PyPI所不包含的任何其他软件包依赖项。

二、Anaconda Python

Anaconda源自Anaconda公司之手(原名为Continuum Analytics),其设计目标在于服务那些需要由商业供应商提供支持且具备企业支持服务的Python开发者。Anaconda Python的主要用例包括数学、统计学、工程、数据分析、机器学习以及其他相关应用。

1.Anaconda Python用例

Anaconda捆绑有Python商业与科学使用场景当中的各类常用库——包括SciPy、NumPy以及Numba等等,同时通过一套定制化软件包管理系统提供更多库访问能力。

Anaconda最为出色的特性在于将上述元素进行了高效组合。在安装之后,Anaconda提供桌面应用程序Anaconda Navigator,可通过方便的GUI帮助用户使用Anaconda环境中的各类功能。相较于CPython,Anaconda当中的组件搜索、更新以及使用流程都更为简便。

其另一大优势,在于Anaconda能够根据特定软件包的需求处理Python生态系统之外的组件。其中专门为Anaconda打造的conda软件包管理器能够根据外部软件要求安装Python以及第三方软件包。

2.Anaconda Python的局限性

由于Anaconda当中包含大量实用性库,且只需要简单操作即可安装更多库,因此Anaconda的安装体积往往要比CPython大得多。基本CPython安装运行大约需要100 MB空间,而Anaconda则会很快增长至GB级别。如果您的资源有限,那么这有可能产生问题。

帮助Anaconda瘦身的方法之一在于安装Miniconda,这是一套精简版本的Anaconda,其中只包含启动与运行所必需的部分。如果必要,您可以将软件包添加到Miniconda当中,并关注各软件包具体要消耗多少空间。

三、ActivePython

与Anaconda类似,ActivePython同样由营利性企业创建及维护——ActiveState公司。该公司还在销售多种语言运行时以及多语言Komodo IDE。

1.ActivePython用例

ActivePython主要面向企业用户与数据科学家——即希望使用Python语言,但又不愿把大量精力浪费在Python的组装与管理方面。ActivePython使用Python中的常规pip软件包管理器,但同时亦以认证压缩包的形式提供数百套通用库,外加英特尔数学核心库等其他一些具有第三方依赖关系的公共库。

2.ActivePython的局限性

ActivePython对软件包外部依赖关系的处理方式存在一大潜在缺点。如果大家希望将现有项目(例如TensorFlow)升级至具有复杂依赖关系的较新版本,则需要同时升级ActivePython。如果开发工作在与特定版本的项目相关联的环境中进行,那么这并不会造成影响。但目前的开发工作往往需要紧跟前沿版本的发布,在这种情况下大家往往会遇到很多麻烦。

四、PyPy

PyPy 属于CPython解释器的替代品,其利用即时(JIT)编译以加速Python程序的执行。根据实际执行的任务情况,其性能提升可能非常显著。

1.PyPy用例

人们对于Python——特别是CPython的抱怨之声,主要围绕其速度表现展开。在默认情况下,Python的运行速度远不及C语言——差距甚至可能达到数百倍。PyPy JIT将Python代码编译为机器语言,从而带来平均7.7倍于CPython的运行速度。在某些特定任务中,其提速效果能够达到50倍。

更重要的是,开发人员能够较为轻松地享受到这些便利。将CPython换成PyPy,您就基本完成了提速工作。

2.PyPy的局限性

PyPy一般更适用于处理“纯”Python应用程序。由于PyPy会模拟CPYthon的原生二进制接口,因此在处理NumPy等包含C库接口的Python软件包时,其表现并不理想。不过随着时间推移,PyPy的开发者们已经逐步解决了这个问题,并使得PyPy能够更好地同依赖于C扩展的Python软件包进行兼容。但必须承认,虽然已经有所改善,PyPy对C扩展的支持仍然有限。

PyPy的另一大短板在于运行时大小。Windows上的核心CPython运行时——不包含标准库——约为4 MB,而PyPy运行时则在32 MB左右。同样需要注意的是,PyPy还长期立足Python的2.x分支,例如目前面向Python 3.x版本的PyPy目前只在32位Windows系统上提供beta测试版本。(PyPy已经在Linux与MacOS上提供同时面向Python 2x与3.x的64位版本。)

五、Jython

JVM(Java虚拟机)能够作为除Java之外的多种语言的运行时选项。这份长的名单包括Groovy、Scala、Clojure、Kotlin、Python以及——没错,当然还有Jython 。

1.Jython用例

Jython项目能够将Python 2.x编译为JVM字节码,并在JVM上运行生成的程序。在某些情况下,Jython编译的程序在运行速度上甚至高于CPython编译程序——但并非始终如此。

Jython提供的最大优势在于能够与Java生态系统中的其他部分直接进行互操作。Java的使用范围比Python更为广泛。通过在JVM上运行Python,开发人员将能够享受并非为Python开发的庞大库与框架生态系统。同样,Jython亦允许Java开发人员使用Python库。

2.Jython的局限性

Jython最大的弊端在于其仅支持Python的2.x版本。目前对Python 3.x版本的支持能力尚在开发当中,但仍需要相当一段时间。当下,还没有任何相关版本放出。

同样需要注意的是,虽然Jython能够将Python引入JVM,但却无法将Python引入Android。由于Jython目前不具备适用于Android的端口,因此Jython不能用于开发Andoird应用。

六、IronPython

类似于Jython的JVM上Python实现方案定位,IronPython属于一套立足.Net运行时——或者CLR(公共语言运行时)——的Python实现方案。IronPython利用CLR的DLR(动态语言运行时)以允许Python程序以等同于CPython的动态水平实现运行。

1.IronPython 用例

与Jython类似,IronPython同样属于一种桥梁。其最主要的用例在于实现Python与.Net间的互操作性。现有.Net程序集能够利用Python的本地导入与对象操作语法在IronPython程序中实现加载。

此外,我们也可以将IronPython代码编译成程序集,并直接运行或者接受其他语言的调用。但需要注意的是,其中的MSIL(微软中间语言)无法直接由其他.Net语言进行访问,因为其并不支持公共语言规范。

2.IronPython的局限性

与Jython类似,IronPython目前只支持Python 2.x版本。不过IronPython 3.x实现方案已经处于紧锣密鼓的开发当中。

Contents

  1. Python Implementations
    1. CPython Variants
      1. Reduced Python Variants
    2. Other Implementations
      1. Working Implementations
      2. Tentative Implementations
    3. Extensions
    4. Compilers
    5. Numerical Accelerators
    6. Similar but Distinct Languages
    7. Topic Guides
    8. (Fun) Python Preprocessors
    9. Academic Projects

Python Implementations

An "implementation" of Python should be taken to mean a program or environment which provides support for the execution of programs written in the Python language, as represented by the CPython reference implementation.

There have been and are several distinct software packages providing what we all recognize as Python, although some of those are more like distributions or variants of some existing implementation than a completely new implementation of the language.

CPython Variants

These are implementations based on the CPython runtime core (the de-facto reference Python implementation), but with extended behaviour or features in some aspects.

  • CrossTwine Linker - a combination of CPython and an add-on library offering improved performance (currently proprietary)

  • Stackless Python - CPython with an emphasis on concurrency using tasklets and channels (used by dspython for the Nintendo DS)

  • Pyston - a fork of CPython, originally developed at Dropbox but now by independent developers, performance-focused including bytecode quickening and a lightweight JIT.

  • Cinder, a fork of CPython by Instagram, containing a number of optimizations like bytecode rewriting and a method-at-a-time JIT compiler.

  • Pyjion, a JIT extension for CPython that compiles Python code to CIL (.NET) and executes it on the CLR.

  • unladen-swallow - "an optimization branch of CPython, intended to be fully compatible and significantly faster", originally considered for merging with CPython subject to PEP 3146, but now unmaintained

  • wpython - a re-implementation of CPython using "wordcode" instead of bytecode

  • eGenix PyRun - Python runtime (CPython + std library) compressed into a single 3-4MB binary

  • x-python - The C Python Interpreter written in Python; useful for educational purposes. There is also bytecode debugger for it

Reduced Python Variants

These provide a subset of the full language + standard library, for use in embedded scenarios (see also the EmbeddedPython topic)

Also look at the sections on Python compilers and extensions below, some of which would qualify as CPython variants.

Other Implementations

These are re-implementations of the Python language that do not depend on (or necessarily interact with) the CPython runtime core. Many of them reuse (a large part of) the standard library implementation.

Note that most of these projects have not yet achieved language compliance. However, many of these have goals and features or run in certain environments that make them interesting in their own regard. The only implementations that are known to be compatible with a given version of the language are IronPythonJython and PyPy.

Working Implementations

The following implementations may be not comprehensive or even complete, but at the very least can be said to be working in that you can run typical programs with them already:

  • PyPy - Python in Python, includes a tracing JIT compiler

  • Jython - Python in Java for the Java platform

  • IronPython - Python in C# for the Common Language Runtime (CLR/.NET) and the FePy project's IronPython Community Edition (IPCE)

  • GraalPython - Python in Java, using the Graal just-in-time compiler and the Truffle interpreter implementation framework

  • Skybison - A C++ from scratch implementation of Python 3.8 using a moving GC, hidden classes and a template JIT.

  • Brython - a way to run Python in the browser through translation to JavaScript

  • CLPython - Python in Common Lisp

  • HotPy - a virtual machine for Python supporting bytecode optimisation and translation (to native code) using type information gathered at runtime

  • pyjs - (formally Pyjamas) a Python to JavaScript compiler plus Web/GUI framework

  • PyMite - Python for embedded devices

  • pyvm - a Python-related virtual machine and software suite providing a nearly self-contained "userspace" system

  • RapydScript - a Python-like language that compiles to JavaScript

  • SNAPpy - "a subset of the Python language that has been optimized for use in low-power embedded devices" (apparently proprietary)

  • tinypy - a minimalist implementation of Python in 64K of code

  • Transcrypt - Python 3.6 to JavaScript precompiler with lean and fast generated code, sourcemaps, built-in minification, optional static type-checking, JSX support

Tentative Implementations

The following implementations are apparent works in progress; they may not be able to run typical programs:

Extensions

These are typically part of CPython (or some other implementation) but change the implementation's behaviour:

  • Psyco - a just-in-time specialising compiler for CPython, abandoned, works only for CPython 2.6.

Compilers

These compilers usually implement something close to Python, although some compilers may impose restrictions that alter the nature of the language:

  • Cython - a widely used optimising Python-to-C compiler, CPython extension module generator, and wrapper language for binding external libraries. Interacts with CPython runtime and supports embedding CPython in stand-alone binaries.

  • Nuitka - a Python-to-C++ compiler using libpython at runtime, attempting some compile-time and run-time optimisations. Interacts with CPython runtime.

  • MyPyC compiles fully typed Python code to a C extension, based on mypy.

  • 2c-python - a static Python-to-C compiler, apparently translating CPython bytecode to C

  • Compyler - an attempt to "transliterate the bytecode into x86 assembly" (now abandoned)

  • GCC Python Front-End - an in-progress effort to compile Python code within the GCC infrastructure

  • Pyc - performs static analysis in order to compile Python programs, uses similar techniques to Shed Skin

  • Shed Skin - a Python-to-C++ compiler, restricted to an implicitly statically typed subset of the language for which it can automatically infer efficient types through whole-program analysis

  • unPython - a Python to C compiler using type annotations

  • VOC - A transpiler that converts Python bytecode into Java bytecode.

Numerical Accelerators

  • Numba - NumPy-aware optimizing runtime compiler for Python

  • Pythran - ahead of time compiler for a subset of Python with a focus on scientific computing

  • Copperhead - purely functional data-parallel Python, compiles to multi-core and GPU backends

  • Parakeet - runtime compiler for a numerical subset of Python

Similar but Distinct Languages

These languages don't attempt to be directly compatible even with a subset of Python, choosing to provide their own set of features:

  • Alore - a compilable language with optional typing and Python/Ruby inspired syntax; an Alore-Python bridge is planned; development effort has been transferred to mypy (see below)

  • Cobra

  • Converge - inspired by Python, Haskell, Icon and Smalltalk, provides macros which can be evaluated at compile-time

  • Delight - based on the D programming language

  • Genie - based on the same foundations (Gtk+, GNOME) as the Vala programming language, supposedly inspired by Boo

  • mypy - Python with optional static typing and some local type inference

  • Mython - an extensible variant of the Python programming language, apparently a front-end for CPython

  • Nim - statically typed, compiles to C, C++, and JS, features parameterised types, macros, and so on

  • Pythonect - a dataflow-oriented language adopting the basic Python expression syntax, implemented in Python and integrated with the Python environment

  • Roket Secured Language - an interpreter for a Python-like language for applications where "restricted Python" execution is desired

  • Serpent - inspired by Python, supporting real-time garbage collection and multiple virtual machines in separate threads (more information)

  • Serpentine - a language with Python-like syntax for the Dalvik virtual machine

  • Wirbel - a compilable language with similar restrictions to Shed Skin (statically typed names, lists cannot mix elements of different types), no longer actively developed as of 2011-07-21

Comparisons:

Topic Guides

(Fun) Python Preprocessors

There are even some tongue-in-cheek dialects of Python which you might find fun.

Academic Projects

Python implementations and compilers have been the topic of various papers and theses. Those that have not apparently been developed further are listed here:


This page aims to replace one formerly maintained as "Cameron Laird's personal notes on varieties of Python implementation". Also of interest will be IntegratingPythonWithOtherLanguages, which, among other variants, mentions embeddings of Python in other languages.


CategoryImplementations

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值