Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "Pure Python?"

Tags:

python

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.

like image 430
djvg Avatar asked Aug 31 '17 08:08

djvg


People also ask

What is Python wheel package?

What Is a Python Wheel? A Python . whl file is essentially a ZIP ( . zip ) archive with a specially crafted filename that tells installers what Python versions and platforms the wheel will support. A wheel is a type of built distribution.

How many Python modules are there?

The Python standard library contains well over 200 modules, although the exact number varies between distributions.


2 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.

like image 123
RemcoGerlich Avatar answered Sep 21 '22 17:09

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/

like image 43
Al Sweigart Avatar answered Sep 22 '22 17:09

Al Sweigart