Python: How to check if an imported module/package/class is from standard library
Question:
Is there a possibility to verify if an import comes from the standard library or not?
For example:
from math import sin #from the standard library.
from my_module import MyClass #not from the standard library.
Answers:
There is no simple way to do this, as the Python standard library is not implemented in a special way – to Python, there is no distinction between the standard library and other modules.
At best, you could use the inspect
module to try and find some indicators, for example, using inspect.getsourcefile()
to find where the source file is located, then using that to check if it’s a core library. This won’t work particularly well, however, as any modules in C will return a TypeError as they are builtins – but you can’t presume they are from the standard library, as any C extension module will do the same thing.
If you really have to do this, my suggestion would be to keep a list of standard library module names and do it that way – it’s not a great solution, but it’s likely to be more stable than any alternative.
If you keep your own modules in a specific directory, you could do something looking at the module’s __file__
property:
>>> import os
>>> os.__file__
'/usr/lib64/python2.6/os.pyc'
>>> import my_module
>>> my_module.__file__
'/path/to/my_packages/.../my_module.pyc'
Below source code will tell you whether a module a standard Python module or not
def is_standard_module(module_name):
if module_name in sys.builtin_module_names:
return True
installation_path = None
try:
installation_path = importlib.import_module(module_name).__file__
except ImportError:
return False
linux_os, _, _ = platform.linux_distribution()
return "dist-packages" not in installation_path if linux_os == "Ubuntu" else "site-packages" not in installation_path
In python 3.10+, you can use sys.stdlib_module_names
import sys
import types
def is_stdlib(module: str | types.ModuleType) -> bool:
"""
Return True if module is in stdlib
"""
if isinstance(module, str):
return module.split('.')[0] in sys.stdlib_module_names
if isinstance(module, types.ModuleType):
return module.__name__.split('.')[0] in sys.stdlib_module_names
raise TypeError('module must be a string or module object')
Is there a possibility to verify if an import comes from the standard library or not?
For example:
from math import sin #from the standard library.
from my_module import MyClass #not from the standard library.
There is no simple way to do this, as the Python standard library is not implemented in a special way – to Python, there is no distinction between the standard library and other modules.
At best, you could use the inspect
module to try and find some indicators, for example, using inspect.getsourcefile()
to find where the source file is located, then using that to check if it’s a core library. This won’t work particularly well, however, as any modules in C will return a TypeError as they are builtins – but you can’t presume they are from the standard library, as any C extension module will do the same thing.
If you really have to do this, my suggestion would be to keep a list of standard library module names and do it that way – it’s not a great solution, but it’s likely to be more stable than any alternative.
If you keep your own modules in a specific directory, you could do something looking at the module’s __file__
property:
>>> import os
>>> os.__file__
'/usr/lib64/python2.6/os.pyc'
>>> import my_module
>>> my_module.__file__
'/path/to/my_packages/.../my_module.pyc'
Below source code will tell you whether a module a standard Python module or not
def is_standard_module(module_name):
if module_name in sys.builtin_module_names:
return True
installation_path = None
try:
installation_path = importlib.import_module(module_name).__file__
except ImportError:
return False
linux_os, _, _ = platform.linux_distribution()
return "dist-packages" not in installation_path if linux_os == "Ubuntu" else "site-packages" not in installation_path
In python 3.10+, you can use sys.stdlib_module_names
import sys
import types
def is_stdlib(module: str | types.ModuleType) -> bool:
"""
Return True if module is in stdlib
"""
if isinstance(module, str):
return module.split('.')[0] in sys.stdlib_module_names
if isinstance(module, types.ModuleType):
return module.__name__.split('.')[0] in sys.stdlib_module_names
raise TypeError('module must be a string or module object')