Repository navigation
Expand file tree
/
Copy path_environment.py
More file actions
78 lines (58 loc) · 2.55 KB
/
Copy path_environment.py
File metadata and controls
78 lines (58 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Part of Spatial Math Toolbox for Python
# Copyright (c) 2000 Peter Corke
# MIT Licence, see details in top-level file: LICENCE
"""
Detect the interactive environment that Python is running in.
This module deliberately does not import Matplotlib, so it is cheap to import
and works when Matplotlib is not installed.
"""
import sys
from typing import Literal
__all__ = ["environment", "isnotebook"]
Environment = Literal["terminal", "ipython", "jupyter", "colab", "jupyterlite"]
def environment() -> Environment:
"""
Determine the environment that Python is running in
:return: the environment
:rtype: str
The result is one of:
======================== ====================================================
``"terminal"`` standard Python interpreter, or script
``"ipython"`` IPython running in a terminal
``"jupyter"`` Jupyter kernel, eg. JupyterLab, Notebook, VS Code or
qtconsole
``"colab"`` Google Colab
``"jupyterlite"`` JupyterLite, running in the browser (Pyodide)
======================== ====================================================
.. note:: The different Jupyter front ends (JupyterLab, classic Notebook,
VS Code, Jupyter Desktop) cannot be reliably told apart from within the
kernel, so all are reported as ``"jupyter"``. How figures will be
rendered depends on the Matplotlib backend, not on the front end.
:seealso: :func:`isnotebook`
"""
# Colab and JupyterLite both also provide an IPython kernel, so test for
# them first
if "google.colab" in sys.modules:
return "colab"
if sys.platform == "emscripten":
return "jupyterlite"
try:
shell = get_ipython().__class__.__name__ # type: ignore[name-defined]
except NameError:
return "terminal" # probably standard Python interpreter
if shell == "ZMQInteractiveShell":
return "jupyter" # Jupyter notebook or qtconsole
elif shell == "TerminalInteractiveShell":
return "ipython"
else:
return "terminal" # other type (?)
def isnotebook() -> bool:
"""
Determine if code is being run from a Jupyter notebook
:return: True if running in a Jupyter kernel, Google Colab or JupyterLite
:rtype: bool
:seealso: :func:`environment`
:references:
- https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook/39662359#39662359
"""
return environment() in ("jupyter", "colab", "jupyterlite")