2007-08-15 11:28:22 -03:00
|
|
|
.. _library-intro:
|
|
|
|
|
|
|
|
************
|
|
|
|
Introduction
|
|
|
|
************
|
|
|
|
|
|
|
|
The "Python library" contains several different kinds of components.
|
|
|
|
|
|
|
|
It contains data types that would normally be considered part of the "core" of a
|
|
|
|
language, such as numbers and lists. For these types, the Python language core
|
|
|
|
defines the form of literals and places some constraints on their semantics, but
|
|
|
|
does not fully define the semantics. (On the other hand, the language core does
|
|
|
|
define syntactic properties like the spelling and priorities of operators.)
|
|
|
|
|
|
|
|
The library also contains built-in functions and exceptions --- objects that can
|
|
|
|
be used by all Python code without the need of an :keyword:`import` statement.
|
|
|
|
Some of these are defined by the core language, but many are not essential for
|
|
|
|
the core semantics and are only described here.
|
|
|
|
|
|
|
|
The bulk of the library, however, consists of a collection of modules. There are
|
|
|
|
many ways to dissect this collection. Some modules are written in C and built
|
|
|
|
in to the Python interpreter; others are written in Python and imported in
|
|
|
|
source form. Some modules provide interfaces that are highly specific to
|
|
|
|
Python, like printing a stack trace; some provide interfaces that are specific
|
|
|
|
to particular operating systems, such as access to specific hardware; others
|
|
|
|
provide interfaces that are specific to a particular application domain, like
|
|
|
|
the World Wide Web. Some modules are available in all versions and ports of
|
|
|
|
Python; others are only available when the underlying system supports or
|
|
|
|
requires them; yet others are available only when a particular configuration
|
|
|
|
option was chosen at the time when Python was compiled and installed.
|
|
|
|
|
|
|
|
This manual is organized "from the inside out:" it first describes the built-in
|
2014-10-11 09:32:34 -03:00
|
|
|
functions, data types and exceptions, and finally the modules, grouped in
|
|
|
|
chapters of related modules.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This means that if you start reading this manual from the start, and skip to the
|
|
|
|
next chapter when you get bored, you will get a reasonable overview of the
|
|
|
|
available modules and application areas that are supported by the Python
|
|
|
|
library. Of course, you don't *have* to read it like a novel --- you can also
|
|
|
|
browse the table of contents (in front of the manual), or look for a specific
|
|
|
|
function, module or term in the index (in the back). And finally, if you enjoy
|
|
|
|
learning about random subjects, you choose a random page number (see module
|
|
|
|
:mod:`random`) and read a section or two. Regardless of the order in which you
|
Merged revisions 73995,74002,74005,74007-74008,74011,74019-74023 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73995 | vinay.sajip | 2009-07-13 07:21:05 -0400 (Mon, 13 Jul 2009) | 1 line
Issue #6314: logging: Extra checks on the "level" argument in more places.
........
r74002 | marc-andre.lemburg | 2009-07-13 16:23:49 -0400 (Mon, 13 Jul 2009) | 6 lines
Use a new global DEV_NULL instead of hard-coding /dev/null into the system
command helper functions.
See #6479 for some motivation.
........
r74005 | marc-andre.lemburg | 2009-07-13 17:28:33 -0400 (Mon, 13 Jul 2009) | 6 lines
Use a different VER command output parser to address the localization
issues mentioned in #3410.
Prepare for Windows 7 (still commented out).
........
r74007 | michael.foord | 2009-07-14 13:58:12 -0400 (Tue, 14 Jul 2009) | 1 line
Move TestRunner initialisation into unittest.TestProgram.runTests. Fixes issue 6418.
........
r74008 | benjamin.peterson | 2009-07-14 20:46:42 -0400 (Tue, 14 Jul 2009) | 1 line
update year
........
r74011 | ezio.melotti | 2009-07-15 13:07:04 -0400 (Wed, 15 Jul 2009) | 1 line
methods' names pep8ification
........
r74019 | amaury.forgeotdarc | 2009-07-15 17:29:27 -0400 (Wed, 15 Jul 2009) | 2 lines
#6076 Add a title to the IDLE Preferences window.
........
r74020 | georg.brandl | 2009-07-16 03:18:07 -0400 (Thu, 16 Jul 2009) | 1 line
#5910: fix kqueue for calls with more than one event.
........
r74021 | georg.brandl | 2009-07-16 03:33:04 -0400 (Thu, 16 Jul 2009) | 1 line
#6486: start with built in functions rather than "built in objects".
........
r74022 | georg.brandl | 2009-07-16 03:38:35 -0400 (Thu, 16 Jul 2009) | 1 line
#6481: fix typo in os.system() replacement.
........
r74023 | jesse.noller | 2009-07-16 10:23:04 -0400 (Thu, 16 Jul 2009) | 1 line
Issue 6433: multiprocessing.pool.map hangs on empty list
........
2009-07-17 06:18:18 -03:00
|
|
|
read the sections of this manual, it helps to start with chapter
|
|
|
|
:ref:`built-in-funcs`, as the remainder of the manual assumes familiarity with
|
|
|
|
this material.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Let the show begin!
|
|
|
|
|
2018-10-12 11:55:20 -03:00
|
|
|
|
|
|
|
.. _availability:
|
|
|
|
|
|
|
|
Notes on availability
|
|
|
|
=====================
|
|
|
|
|
|
|
|
* An "Availability: Unix" note means that this function is commonly found on
|
|
|
|
Unix systems. It does not make any claims about its existence on a specific
|
|
|
|
operating system.
|
|
|
|
|
|
|
|
* If not separately noted, all functions that claim "Availability: Unix" are
|
2024-09-23 20:09:53 -03:00
|
|
|
supported on macOS, iOS and Android, all of which build on a Unix core.
|
2018-10-12 11:55:20 -03:00
|
|
|
|
2022-07-29 11:42:09 -03:00
|
|
|
* If an availability note contains both a minimum Kernel version and a minimum
|
|
|
|
libc version, then both conditions must hold. For example a feature with note
|
|
|
|
*Availability: Linux >= 3.17 with glibc >= 2.27* requires both Linux 3.17 or
|
|
|
|
newer and glibc 2.27 or newer.
|
2022-08-02 16:00:41 -03:00
|
|
|
|
|
|
|
.. _wasm-availability:
|
|
|
|
|
|
|
|
WebAssembly platforms
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
The `WebAssembly`_ platforms ``wasm32-emscripten`` (`Emscripten`_) and
|
|
|
|
``wasm32-wasi`` (`WASI`_) provide a subset of POSIX APIs. WebAssembly runtimes
|
|
|
|
and browsers are sandboxed and have limited access to the host and external
|
|
|
|
resources. Any Python standard library module that uses processes, threading,
|
|
|
|
networking, signals, or other forms of inter-process communication (IPC), is
|
|
|
|
either not available or may not work as on other Unix-like systems. File I/O,
|
|
|
|
file system, and Unix permission-related functions are restricted, too.
|
|
|
|
Emscripten does not permit blocking I/O. Other blocking operations like
|
|
|
|
:func:`~time.sleep` block the browser event loop.
|
|
|
|
|
|
|
|
The properties and behavior of Python on WebAssembly platforms depend on the
|
|
|
|
`Emscripten`_-SDK or `WASI`_-SDK version, WASM runtimes (browser, NodeJS,
|
|
|
|
`wasmtime`_), and Python build time flags. WebAssembly, Emscripten, and WASI
|
|
|
|
are evolving standards; some features like networking may be
|
|
|
|
supported in the future.
|
|
|
|
|
|
|
|
For Python in the browser, users should consider `Pyodide`_ or `PyScript`_.
|
|
|
|
PyScript is built on top of Pyodide, which itself is built on top of
|
|
|
|
CPython and Emscripten. Pyodide provides access to browsers' JavaScript and
|
|
|
|
DOM APIs as well as limited networking capabilities with JavaScript's
|
|
|
|
``XMLHttpRequest`` and ``Fetch`` APIs.
|
|
|
|
|
|
|
|
* Process-related APIs are not available or always fail with an error. That
|
|
|
|
includes APIs that spawn new processes (:func:`~os.fork`,
|
|
|
|
:func:`~os.execve`), wait for processes (:func:`~os.waitpid`), send signals
|
|
|
|
(:func:`~os.kill`), or otherwise interact with processes. The
|
|
|
|
:mod:`subprocess` is importable but does not work.
|
|
|
|
|
|
|
|
* The :mod:`socket` module is available, but is limited and behaves
|
|
|
|
differently from other platforms. On Emscripten, sockets are always
|
|
|
|
non-blocking and require additional JavaScript code and helpers on the
|
|
|
|
server to proxy TCP through WebSockets; see `Emscripten Networking`_
|
|
|
|
for more information. WASI snapshot preview 1 only permits sockets from an
|
|
|
|
existing file descriptor.
|
|
|
|
|
|
|
|
* Some functions are stubs that either don't do anything and always return
|
|
|
|
hardcoded values.
|
|
|
|
|
|
|
|
* Functions related to file descriptors, file permissions, file ownership, and
|
|
|
|
links are limited and don't support some operations. For example, WASI does
|
|
|
|
not permit symlinks with absolute file names.
|
|
|
|
|
|
|
|
.. _WebAssembly: https://webassembly.org/
|
|
|
|
.. _Emscripten: https://emscripten.org/
|
2022-11-18 14:57:04 -04:00
|
|
|
.. _Emscripten Networking: https://emscripten.org/docs/porting/networking.html
|
2022-08-02 16:00:41 -03:00
|
|
|
.. _WASI: https://wasi.dev/
|
|
|
|
.. _wasmtime: https://wasmtime.dev/
|
|
|
|
.. _Pyodide: https://pyodide.org/
|
|
|
|
.. _PyScript: https://pyscript.net/
|
2024-03-28 05:13:13 -03:00
|
|
|
|
2024-09-23 20:09:53 -03:00
|
|
|
.. _mobile-availability:
|
2024-03-28 05:13:13 -03:00
|
|
|
.. _iOS-availability:
|
|
|
|
|
2024-09-23 20:09:53 -03:00
|
|
|
Mobile platforms
|
|
|
|
----------------
|
2024-03-28 05:13:13 -03:00
|
|
|
|
2024-09-23 20:09:53 -03:00
|
|
|
Android and iOS are, in most respects, POSIX operating systems. File I/O, socket handling,
|
2024-03-28 05:13:13 -03:00
|
|
|
and threading all behave as they would on any POSIX operating system. However,
|
2024-09-23 20:09:53 -03:00
|
|
|
there are several major differences:
|
|
|
|
|
|
|
|
* Mobile platforms can only use Python in "embedded" mode. There is no Python
|
|
|
|
REPL, and no ability to use separate executables such as :program:`python` or
|
|
|
|
:program:`pip`. To add Python code to your mobile app, you must use
|
|
|
|
the :ref:`Python embedding API <embedding>`. For more details, see
|
|
|
|
:ref:`using-android` and :ref:`using-ios`.
|
|
|
|
|
|
|
|
* Subprocesses:
|
|
|
|
|
|
|
|
* On Android, creating subprocesses is possible but `officially unsupported
|
|
|
|
<https://issuetracker.google.com/issues/128554619#comment4>`__.
|
|
|
|
In particular, Android does not support any part of the System V IPC API,
|
|
|
|
so :mod:`multiprocessing` is not available.
|
|
|
|
|
|
|
|
* An iOS app cannot use any form of subprocessing, multiprocessing, or
|
|
|
|
inter-process communication. If an iOS app attempts to create a subprocess,
|
|
|
|
the process creating the subprocess will either lock up, or crash. An iOS app
|
|
|
|
has no visibility of other applications that are running, nor any ability to
|
|
|
|
communicate with other running applications, outside of the iOS-specific APIs
|
|
|
|
that exist for this purpose.
|
|
|
|
|
|
|
|
* Mobile apps have limited access to modify system resources (such as the system
|
2024-03-28 05:13:13 -03:00
|
|
|
clock). These resources will often be *readable*, but attempts to modify
|
|
|
|
those resources will usually fail.
|
|
|
|
|
2024-09-23 20:09:53 -03:00
|
|
|
* Console input and output:
|
|
|
|
|
|
|
|
* On Android, the native ``stdout`` and ``stderr`` are not connected to
|
|
|
|
anything, so Python installs its own streams which redirect messages to the
|
|
|
|
system log. These can be seen under the tags ``python.stdout`` and
|
|
|
|
``python.stderr`` respectively.
|
|
|
|
|
|
|
|
* iOS apps have a limited concept of console output. ``stdout`` and
|
|
|
|
``stderr`` *exist*, and content written to ``stdout`` and ``stderr`` will be
|
|
|
|
visible in logs when running in Xcode, but this content *won't* be recorded
|
|
|
|
in the system log. If a user who has installed your app provides their app
|
|
|
|
logs as a diagnostic aid, they will not include any detail written to
|
|
|
|
``stdout`` or ``stderr``.
|
2024-03-28 05:13:13 -03:00
|
|
|
|
2024-09-23 20:09:53 -03:00
|
|
|
* Mobile apps have no usable ``stdin`` at all. While apps can display an on-screen
|
|
|
|
keyboard, this is a software feature, not something that is attached to
|
|
|
|
``stdin``.
|
2024-03-28 05:13:13 -03:00
|
|
|
|
2024-09-23 20:09:53 -03:00
|
|
|
As a result, Python modules that involve console manipulation (such as
|
|
|
|
:mod:`curses` and :mod:`readline`) are not available on mobile platforms.
|