18

Many questions on Stack Overflow refer to "Pure Python" (some random examples from the "similar questions" list: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11).

I also encounter the concept elsewhere on the web, e.g. in the package documentation for imageio and in tutorials such as "An introduction to Pure Python".

This has led me to believe there must be some universally accepted standard definition of what "Pure Python" is.

However, despite googling to the limits of my ability, I have not yet been able to locate this definition.

Is there a universally accepted definition of "Pure Python," or is this just some elusive concept that means different things to different people?

To be clear, I am asking: Does such a definition exist, yes or no, and if so, what is the acclaimed source? Although I truly appreciate all comments and answers, I am not looking for personal interpretations.

djvg
  • 11,722
  • 5
  • 72
  • 103
  • What is "pure" is as personal as what is "beautiful". – Klaus D. Aug 31 '17 at 08:40
  • 3
    To me 'Pure Python' means no external packages need installing, but imports from the standard library are fine – Chris_Rands Aug 31 '17 at 08:40
  • 1
    This is a very broad question. But in general, pure python refers to anything that does not need `pip install ...` to use. – cs95 Aug 31 '17 at 08:40
  • 1
    This is not a well-defined term. Even in the questions you linked to, people are using it to mean different things. – Peter Hall Aug 31 '17 at 08:40
  • 1
    Usually it meens written only in python, without modules implemented in C (or other languages for python implementations other then cPython) – mata Aug 31 '17 at 08:43
  • @Chris_Rands - Thanks. That is more or less what I was thinking. Do you know of a reliable source for this, or is it "common knowledge?" – djvg Aug 31 '17 at 08:53
  • @cᴏʟᴅsᴘᴇᴇᴅ - Thanks. Although I thought asking for a "universally accepted standard definition" was not a "very broad question." It is a question of existence, yes or no. Not looking for opinions. Your comment/answer might be the one, but is there an acclaimed source for it? – djvg Aug 31 '17 at 09:01
  • 1
    @Dennis I don't think you will find this officially documented. I've only ever seen "implemented in pure python" meaning python only without [extension modules](https://docs.python.org/3/glossary.html#term-extension-module). The other meaning presented here is usually called "stdlib only" ("standard library only") – mata Aug 31 '17 at 09:08
  • 1
    I doubt there is a formal definition. At minimum, it suggests that Python is the sole language applied in code base. At most, it can imply that an implementation relies on no other dependencies than the standard library. It seems the term "pure Python" is less ubiquitous and potent than the term "Pythonic", which is rooted in the Zen of Python, reinforced by a few important PEPs and supported by some accepted idiomatic practices. – pylang Aug 31 '17 at 09:23

3 Answers3

21

In that imageio package, they mean it's all implemented in Python, and not (as is sometimes done) with parts written in C or other languages. As a result it's guaranteed to work on any system that Python works on.

In that tutorial, it means the Python you get when you download and install Python -- the language and the standard libraries, not any external modules. The chapter after that adds some external libraries, like numpy and scipy, that are used a lot but aren't part of the standard library.

So they mean different things there already.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • 4
    I've seen the first definition used consistently in many places, to the point that I'd say it is the accepted meaning. I think that tutorial is an exception (the author misunderstood the term or just used the two words together without realising they are commonly used) rather than an equally valid meaning. – Arthur Tacca Aug 31 '17 at 08:48
  • 1
    @ArthurTaca: well there's also the broader use, if you use "pure Javascript" then it means not using external libraries or frameworks like jQuery or React, even if those are themselves just Javascript and not C or so. It depends on context. – RemcoGerlich Aug 31 '17 at 09:54
  • In the context of python "pure python" is often used to hint that a library will be compatible with not just cPython but other implemetations as well (usually that will explained explicitly too) - example: [PyMySQL](https://github.com/PyMySQL/PyMySQL). [paramiko](http://www.paramiko.org/) for a different example also uses the term, but itself requires a C extension module to work... "pure" simply isn't a defined technical term, so there are no wrong or right answers to that question. – mata Aug 31 '17 at 10:15
  • The second meaning is more often referenced as "vanilla Python", ie, CPython with only standard libraries. – gaborous Dec 10 '22 at 17:32
6

A "pure-Python" package is a package that only contains Python code, and doesn't include, say, C extensions or code in other languages. You only need a Python interpreter and the Python Standard Library to run a pure-Python package, and it doesn't matter what your OS or platform is.

Pure-Python packages can import or depend on non-pure-Python packages:

  • Package X contains only Python code and is a pure-Python package.
  • Package Y contains Python and C code and isn't a pure-Python package.
  • Package Z imports Package Y, but Package Z is still a pure-Python package.

A good rule of thumb: If you can make a source distribution ("sdist") of your package and it doesn't include any non-Python code, it is a pure-Python package.

Pure-Python packages aren't restricted to just the Python Standard Library; packages can import modules from outside the Python Standard Library and still be considered pure-Python.

Additionally, a standalone module is a single .py file that only imports modules from the Python Standard Library. A standalone module is necessarily a pure-Python module.

Note that in Python, package technically refers to a folder with an __init__.py file in it. The things you download and install from PyPI with pip are distributions (such as "source distribution" or "sdist"), though the term "package" is also used as a synonym with "distribution", since that term could be confused with the "Linux distro" usage of the word.

Is there an official definition for "pure-Python"? As of this writing, no, though the Python Packaging User Guide makes heavy use of the term in https://packaging.python.org/overview/

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
  • 1
    "Pure-Python packages can import or depend on non-pure-Python packages" -> I disagree: if the core logic of what your package is doing requires a non pure Python package, then your package is essentially just a wrapper, and it can't be called pure Python. A "pure Python" package implements the core logic only in Pythonic statements that only require pure python packages IMHO. – gaborous Dec 10 '22 at 17:35
0

Unfortunately, it seems there is no standardized, formalized definition.

As a programmer in Python for almost 2 decades, my definition of pure Python is: a Python package that implements the core logic only in Pythonic statements that only require pure python or native packages. This is a recursive statement, so at the far end of your packages dependency tree, you end up with python packages that only require native python libraries/functions. With this approach, the whole chain of code logic that allows for the main objective of the tool to be accomplished can be read and modified only using Python, and no other programming language nor tool besides the CPython interpreter.

This can't be overstated: "pure Python" is more defined as an objective -- of being entirely readable and modifiable in the Python language --, rather than a state. It's very similar to what the sibling language Julia is trying to do but by generalizing the procedure down to the interpreter, which is written in the Julia language itself. You can say that Julia is "pure" by design, whereas CPython is not (because the CPython interpreter is compiled in C++), but you can still write "pure Python" packages, just like you can write "pure PHP" or "pure Ruby" packages that do not require the use of any package written in another language at any point of the program's logic.

gaborous
  • 15,832
  • 10
  • 83
  • 102