xref: /third_party/python/Doc/library/intro.rst (revision 7db96d56)
17db96d56Sopenharmony_ci.. _library-intro:
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci************
47db96d56Sopenharmony_ciIntroduction
57db96d56Sopenharmony_ci************
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ciThe "Python library" contains several different kinds of components.
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ciIt contains data types that would normally be considered part of the "core" of a
107db96d56Sopenharmony_cilanguage, such as numbers and lists.  For these types, the Python language core
117db96d56Sopenharmony_cidefines the form of literals and places some constraints on their semantics, but
127db96d56Sopenharmony_cidoes not fully define the semantics.  (On the other hand, the language core does
137db96d56Sopenharmony_cidefine syntactic properties like the spelling and priorities of operators.)
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ciThe library also contains built-in functions and exceptions --- objects that can
167db96d56Sopenharmony_cibe used by all Python code without the need of an :keyword:`import` statement.
177db96d56Sopenharmony_ciSome of these are defined by the core language, but many are not essential for
187db96d56Sopenharmony_cithe core semantics and are only described here.
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ciThe bulk of the library, however, consists of a collection of modules. There are
217db96d56Sopenharmony_cimany ways to dissect this collection.  Some modules are written in C and built
227db96d56Sopenharmony_ciin to the Python interpreter; others are written in Python and imported in
237db96d56Sopenharmony_cisource form.  Some modules provide interfaces that are highly specific to
247db96d56Sopenharmony_ciPython, like printing a stack trace; some provide interfaces that are specific
257db96d56Sopenharmony_cito particular operating systems, such as access to specific hardware; others
267db96d56Sopenharmony_ciprovide interfaces that are specific to a particular application domain, like
277db96d56Sopenharmony_cithe World Wide Web. Some modules are available in all versions and ports of
287db96d56Sopenharmony_ciPython; others are only available when the underlying system supports or
297db96d56Sopenharmony_cirequires them; yet others are available only when a particular configuration
307db96d56Sopenharmony_cioption was chosen at the time when Python was compiled and installed.
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ciThis manual is organized "from the inside out:" it first describes the built-in
337db96d56Sopenharmony_cifunctions, data types and exceptions, and finally the modules, grouped in
347db96d56Sopenharmony_cichapters of related modules.
357db96d56Sopenharmony_ci
367db96d56Sopenharmony_ciThis means that if you start reading this manual from the start, and skip to the
377db96d56Sopenharmony_cinext chapter when you get bored, you will get a reasonable overview of the
387db96d56Sopenharmony_ciavailable modules and application areas that are supported by the Python
397db96d56Sopenharmony_cilibrary.  Of course, you don't *have* to read it like a novel --- you can also
407db96d56Sopenharmony_cibrowse the table of contents (in front of the manual), or look for a specific
417db96d56Sopenharmony_cifunction, module or term in the index (in the back).  And finally, if you enjoy
427db96d56Sopenharmony_cilearning about random subjects, you choose a random page number (see module
437db96d56Sopenharmony_ci:mod:`random`) and read a section or two.  Regardless of the order in which you
447db96d56Sopenharmony_ciread the sections of this manual, it helps to start with chapter
457db96d56Sopenharmony_ci:ref:`built-in-funcs`, as the remainder of the manual assumes familiarity with
467db96d56Sopenharmony_cithis material.
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ciLet the show begin!
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ci
517db96d56Sopenharmony_ci.. _availability:
527db96d56Sopenharmony_ci
537db96d56Sopenharmony_ciNotes on availability
547db96d56Sopenharmony_ci=====================
557db96d56Sopenharmony_ci
567db96d56Sopenharmony_ci* An "Availability: Unix" note means that this function is commonly found on
577db96d56Sopenharmony_ci  Unix systems.  It does not make any claims about its existence on a specific
587db96d56Sopenharmony_ci  operating system.
597db96d56Sopenharmony_ci
607db96d56Sopenharmony_ci* If not separately noted, all functions that claim "Availability: Unix" are
617db96d56Sopenharmony_ci  supported on macOS, which builds on a Unix core.
627db96d56Sopenharmony_ci
637db96d56Sopenharmony_ci* If an availability note contains both a minimum Kernel version and a minimum
647db96d56Sopenharmony_ci  libc version, then both conditions must hold. For example a feature with note
657db96d56Sopenharmony_ci  *Availability: Linux >= 3.17 with glibc >= 2.27* requires both Linux 3.17 or
667db96d56Sopenharmony_ci  newer and glibc 2.27 or newer.
677db96d56Sopenharmony_ci
687db96d56Sopenharmony_ci.. _wasm-availability:
697db96d56Sopenharmony_ci
707db96d56Sopenharmony_ciWebAssembly platforms
717db96d56Sopenharmony_ci---------------------
727db96d56Sopenharmony_ci
737db96d56Sopenharmony_ciThe `WebAssembly`_ platforms ``wasm32-emscripten`` (`Emscripten`_) and
747db96d56Sopenharmony_ci``wasm32-wasi`` (`WASI`_) provide a subset of POSIX APIs. WebAssembly runtimes
757db96d56Sopenharmony_ciand browsers are sandboxed and have limited access to the host and external
767db96d56Sopenharmony_ciresources. Any Python standard library module that uses processes, threading,
777db96d56Sopenharmony_cinetworking, signals, or other forms of inter-process communication (IPC), is
787db96d56Sopenharmony_cieither not available or may not work as on other Unix-like systems. File I/O,
797db96d56Sopenharmony_cifile system, and Unix permission-related functions are restricted, too.
807db96d56Sopenharmony_ciEmscripten does not permit blocking I/O. Other blocking operations like
817db96d56Sopenharmony_ci:func:`~time.sleep` block the browser event loop.
827db96d56Sopenharmony_ci
837db96d56Sopenharmony_ciThe properties and behavior of Python on WebAssembly platforms depend on the
847db96d56Sopenharmony_ci`Emscripten`_-SDK or `WASI`_-SDK version, WASM runtimes (browser, NodeJS,
857db96d56Sopenharmony_ci`wasmtime`_), and Python build time flags. WebAssembly, Emscripten, and WASI
867db96d56Sopenharmony_ciare evolving standards; some features like networking may be
877db96d56Sopenharmony_cisupported in the future.
887db96d56Sopenharmony_ci
897db96d56Sopenharmony_ciFor Python in the browser, users should consider `Pyodide`_ or `PyScript`_.
907db96d56Sopenharmony_ciPyScript is built on top of Pyodide, which itself is built on top of
917db96d56Sopenharmony_ciCPython and Emscripten. Pyodide provides access to browsers' JavaScript and
927db96d56Sopenharmony_ciDOM APIs as well as limited networking capabilities with JavaScript's
937db96d56Sopenharmony_ci``XMLHttpRequest`` and ``Fetch`` APIs.
947db96d56Sopenharmony_ci
957db96d56Sopenharmony_ci* Process-related APIs are not available or always fail with an error. That
967db96d56Sopenharmony_ci  includes APIs that spawn new processes (:func:`~os.fork`,
977db96d56Sopenharmony_ci  :func:`~os.execve`), wait for processes (:func:`~os.waitpid`), send signals
987db96d56Sopenharmony_ci  (:func:`~os.kill`), or otherwise interact with processes. The
997db96d56Sopenharmony_ci  :mod:`subprocess` is importable but does not work.
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ci* The :mod:`socket` module is available, but is limited and behaves
1027db96d56Sopenharmony_ci  differently from other platforms. On Emscripten, sockets are always
1037db96d56Sopenharmony_ci  non-blocking and require additional JavaScript code and helpers on the
1047db96d56Sopenharmony_ci  server to proxy TCP through WebSockets; see `Emscripten Networking`_
1057db96d56Sopenharmony_ci  for more information. WASI snapshot preview 1 only permits sockets from an
1067db96d56Sopenharmony_ci  existing file descriptor.
1077db96d56Sopenharmony_ci
1087db96d56Sopenharmony_ci* Some functions are stubs that either don't do anything and always return
1097db96d56Sopenharmony_ci  hardcoded values.
1107db96d56Sopenharmony_ci
1117db96d56Sopenharmony_ci* Functions related to file descriptors, file permissions, file ownership, and
1127db96d56Sopenharmony_ci  links are limited and don't support some operations. For example, WASI does
1137db96d56Sopenharmony_ci  not permit symlinks with absolute file names.
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ci.. _WebAssembly: https://webassembly.org/
1167db96d56Sopenharmony_ci.. _Emscripten: https://emscripten.org/
1177db96d56Sopenharmony_ci.. _Emscripten Networking: https://emscripten.org/docs/porting/networking.html
1187db96d56Sopenharmony_ci.. _WASI: https://wasi.dev/
1197db96d56Sopenharmony_ci.. _wasmtime: https://wasmtime.dev/
1207db96d56Sopenharmony_ci.. _Pyodide: https://pyodide.org/
1217db96d56Sopenharmony_ci.. _PyScript: https://pyscript.net/
122