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.

9
  • 3
    To me 'Pure Python' means no external packages need installing, but imports from the standard library are fine Aug 31, 2017 at 8: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, 2017 at 8: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, 2017 at 8: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, 2017 at 8:43
  • 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. The other meaning presented here is usually called "stdlib only" ("standard library only")
    – mata
    Aug 31, 2017 at 9:08

3 Answers 3

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.

4
  • 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. Aug 31, 2017 at 8: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. Aug 31, 2017 at 9: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. paramiko 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, 2017 at 10:15
  • The second meaning is more often referenced as "vanilla Python", ie, CPython with only standard libraries.
    – gaborous
    Dec 10, 2022 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/

1
  • 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, 2022 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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.