* ``-X importtime`` to show how long each import takes. It shows module name,
cumulative time (including nested imports) and self time (exluding nested
imports). Note that its output may be broken in multi threaded application.
- Typical usage is ``python3 -X importtime -c 'import asyncio'``.
+ Typical usage is ``python3 -X importtime -c 'import asyncio'``. See also
+ :envvar:`PYTHONPROFILEIMPORTTIME`.
It also allows passing arbitrary values and retrieving them through the
:data:`sys._xoptions` dictionary.
The ``-X showalloccount`` option.
.. versionadded:: 3.7
- The ``-X importtime`` option.
+ The ``-X importtime`` and :envvar:`PYTHONPROFILEIMPORTTIME` options.
Options you shouldn't use
.. versionadded:: 3.4
+.. envvar:: PYTHONPROFILEIMPORTTIME
+
+ If this environment variable is set to a non-empty string, Python will
+ show how long each import takes. This is exactly equivalent to setting
+ ``-X importtime`` on the command line.
+
+ .. versionadded:: 3.7
+
+
.. envvar:: PYTHONASYNCIODEBUG
If this environment variable is set to a non-empty string, enable the
.. section: Core and Builtins
Add ``-X importtime`` option to show how long each import takes. It can be
-used to optimize application's startup time.
+used to optimize application's startup time. Support the
+:envvar:`PYTHONPROFILEIMPORTTIME` as an equivalent way to enable this.
..
* _PyDict_GetItemId()
*/
if (ximporttime == 0) {
- PyObject *xoptions = PySys_GetXOptions();
- if (xoptions) {
- PyObject *value = _PyDict_GetItemId(xoptions, &PyId_importtime);
- ximporttime = (value == Py_True);
+ char *envoption = Py_GETENV("PYTHONPROFILEIMPORTTIME");
+ if (envoption != NULL && strlen(envoption) > 0) {
+ ximporttime = 1;
+ }
+ else {
+ PyObject *xoptions = PySys_GetXOptions();
+ if (xoptions) {
+ PyObject *value = _PyDict_GetItemId(
+ xoptions, &PyId_importtime);
+ ximporttime = (value == Py_True);
+ }
}
if (ximporttime) {
fputs("import time: self [us] | cumulative | imported package\n",