Blocking Imports#
There are two ways to configure how imports are blocked or allowed.
allowed_imports- Takes a list of module names. If this option is set, only imports in this list can be importedblocked_imports- Takes a list of module names. If this option is set, all inputs specified are blocked. Ifallowed_importsis set,blocked_importswill be ignored.
Example of using allowed_imports:
ipython_config.py#
# In ipython_config.py
c = get_config()
c.ExamKernel.allowed_imports = ["math", "random"]
Example of trying to import a module that is not in the allowed imports#
import os
---------------------------------------------------------------------------
ModuleNotFoundError: No module named os or os blocked by kernel.
Allowed imports are: [math, random]
Example of using blocked_imports:
ipython_config.py#
# In ipython_config.py
c = get_config()
c.ExamKernel.blocked_imports = ["os", "pandas"]
Example of trying to import a module that is in the blocked imports#
import os
---------------------------------------------------------------------------
ModuleNotFoundError: No module named os or os blocked by kernel.
The following imports are blocked: [os, pandas]