What is "Pure Python?"

Question:

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.

Asked By: djvg

||

Answers:

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.

Answered By: RemcoGerlich

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/

Answered By: Al Sweigart

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.

Answered By: gaborous
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.