Import of an unknown module in Python - python

An example of what I need on Node.js
How can you import an unknown module into Python?
I need to be able to check the existence of a module and import it if it exists, or send a message that the module is not found.
I hope for your help >-<

I think this is what you are looking for:
try:
import {Package-to-import}
except ModuleNotFoundError:
{Do something if import fails}
If you want some context from the exception you can use the following for the except statement
except ModuleNotFoundError as exc:
exc will be the instance of the raised exception.

for example:
lib = 'sqlite3'
exec(f'import {lib}')
print(sqlite3.connect)

Related

Python file import doesn't appear in namespace prior to exception, but does after exception

I have a snippet of Python code that throws an exception "No module named 'common_functions'" when I try to import the file "common_functions.py" from folder API.
def validate_config():
try:
import API
logger.debug(dir(API))
from API.common_functions import get_dbusername
except Exception as e:
import API
logger.debug(dir(API))
from API.common_functions import get_dbusername
When I look at the output of the logging of the modules present in API initially it is:
[..., 'config', 'external_config_operations']
but in the exception the missing module is present:
[ ..., 'common_functions', 'config','external_config_operations']
I've tried clearing out all cached files and for some reason this is still happening. Does anyone know why module discovery is working this way?

Is it possible to import a module using a string in python? [duplicate]

This question already has answers here:
Importing Python libraries and gracefully handling if they are not availalble
(3 answers)
Closed 2 years ago.
Is there any chance to use the snippets given below? When I run this code, it returns "module pandas is not installed"
a = "pandas"
try:
import a
print("module ",a," is installed")
except ModuleNotFoundError:
print("module ",a," is not installed")
But when I run the code given below:
try:
import pandas
print("module pandas is installed")
except ModuleNotFoundError:
print("module pandas is not installed")
It returns "module pandas is installed".
What is the difference between them?
In the first example, what you're doing is more equivalent to the following:
try:
import "pandas"
print("module ",a," is installed")
except ModuleNotFoundError:
print("module ",a," is not installed")
You can't import a string, unless you use importlib. You can find documentation for that library here. That would look something like the following:
import importlib
a = "pandas"
try:
globals()[a] = importlib.import_module(a)
print("module ",a," is installed")
except:
print("module ",a," is not installed")
You have to execute the script as one combined formatted string, using exec(), this actually executes the script:
a = "pandas"
try:
exec(f'import {a}')
print("module ",a," is installed")
except ModuleNotFoundError:
print("module ",a," is not installed")
To read more about the Python exec() function, visit:
https://www.w3schools.com/python/ref_func_exec.asp
In your first example, you import a module named a (you cannot use variables in an import statement), which results in ModuleNotFoundError. In your second example, you import a module named pandas.
If you want to dynamically import a module (e.g. based on user input), use importlib:
import importlib
name = 'pandas'
try:
module = importlib.import_module(name)
print(f'Module {name} is installed')
except ModuleNotFoundError:
print(f'Module {name} is not installed')
Do not use eval or exec - they are not safe.
#Jaz has successfully shown what is wrong with your first approach. You can't import a module by variable name using the import statement.
In general, python philosophy is to ask for forgiveness, not permission. So you often see the following construction in common modules that have optional dependencies:
try:
import pandas
except ImportError:
print("pandas is not installed")
else:
print("pandas is installed")
The programmatic interface to the import statement is the built-in __import__ function. You can use it something like this:
m = 'pandas'
try:
locals()[m] = __import__(m)
except ImportError:
...
A simplified interface is available in the functions of importlib.

python how to check if a module exists without importing it [duplicate]

How can I know if a Python module exists, without importing it?
Importing something that might not exist (not what I want) results in:
try:
import eggs
except ImportError:
pass
TL;DR) Use importlib.util.find_spec(module_name) (Python 3.4+).
Python2: imp.find_module
To check if import can find something in Python 2, using imp:
import imp
try:
imp.find_module('eggs')
found = True
except ImportError:
found = False
To find dotted imports, you need to do more:
import imp
try:
spam_info = imp.find_module('spam')
spam = imp.load_module('spam', *spam_info)
imp.find_module('eggs', spam.__path__) # __path__ is already a list
found = True
except ImportError:
found = False
You can also use pkgutil.find_loader (more or less the same as the Python 3 part:
import pkgutil
eggs_loader = pkgutil.find_loader('eggs')
found = eggs_loader is not None
Python 3
Python 3 ≤ 3.3: importlib.find_loader
You should use importlib. I went about doing this like:
import importlib
spam_loader = importlib.find_loader('spam')
found = spam_loader is not None
My expectation being, if you can find a loader for it, then it exists. You can also be a bit more smart about it, like filtering out what loaders you will accept. For example:
import importlib
spam_loader = importlib.find_loader('spam')
# only accept it as valid if there is a source file for the module - no bytecode only.
found = issubclass(type(spam_loader), importlib.machinery.SourceFileLoader)
Python 3 ≥ 3.4: importlib.util.find_spec
In Python 3.4 importlib.find_loader Python documentation was deprecated in favour of importlib.util.find_spec. The recommended method is the importlib.util.find_spec. There are others like importlib.machinery.FileFinder, which is useful if you're after a specific file to load. Figuring out how to use them is beyond the scope of this.
import importlib
spam_spec = importlib.util.find_spec("spam")
found = spam_spec is not None
This also works with relative imports, but you must supply the starting package, so you could also do:
import importlib
spam_spec = importlib.util.find_spec("..spam", package="eggs.bar")
found = spam_spec is not None
spam_spec.name == "eggs.spam"
While I'm sure there exists a reason for doing this - I'm not sure what it would be.
Warning
When trying to find a submodule, it will import the parent module (for ALL of the above methods)!
food/
|- __init__.py
|- eggs.py
## __init__.py
print("module food loaded")
## eggs.py
print("module eggs")
were you then to run
>>> import importlib
>>> spam_spec = importlib.util.find_spec("food.eggs")
module food loaded
ModuleSpec(name='food.eggs', loader=<_frozen_importlib.SourceFileLoader object at 0x10221df28>, origin='/home/user/food/eggs.py')
Comments are welcome on getting around this
Acknowledgements
#rvighne for importlib
#lucas-guido for Python 3.3+ deprecating find_loader
#enpenax for pkgutils.find_loader behaviour in Python 2.7
Python 3 >= 3.6: ModuleNotFoundError
The ModuleNotFoundError has been introduced in Python 3.6 and can be used for this purpose:
try:
import eggs
except ModuleNotFoundError:
# Error handling
pass
The error is raised when a module or one of its parents cannot be found. So
try:
import eggs.sub
except ModuleNotFoundError as err:
# Error handling
print(err)
would print a message that looks like No module named 'eggs' if the eggs module cannot be found; but it would print something like No module named 'eggs.sub' if only the sub module couldn't be found, but the eggs package could be found.
See the documentation of the import system for more information on the ModuleNotFoundError.
After using yarbelk's response, I've made this so I don't have to import ìmp.
try:
__import__('imp').find_module('eggs')
# Make things with a supposed existing module
except ImportError:
pass
It is useful in Django's settings.py file, for example.
Python 2, without relying on ImportError
Until the current answer is updated, here is the way for Python 2
import pkgutil
import importlib
if pkgutil.find_loader(mod) is not None:
return importlib.import_module(mod)
return None
Why another answer?
A lot of answers make use of catching an ImportError. The problem with that is, that we cannot know what throws the ImportError.
If you import your existent module and there happens to be an ImportError in your module (e.g., typo on line 1), the result will be that your module does not exist.
It will take you quite the amount of backtracking to figure out that your module exists and the ImportError is caught and makes things fail silently.
go_as's answer as a one-liner:
python -c "help('modules');" | grep module
Use one of the functions from pkgutil, for example:
from pkgutil import iter_modules
def module_exists(module_name):
return module_name in (name for loader, name, ispkg in iter_modules())
I wrote this helper function:
def is_module_available(module_name):
if sys.version_info < (3, 0):
# python 2
import importlib
torch_loader = importlib.find_loader(module_name)
elif sys.version_info <= (3, 3):
# python 3.0 to 3.3
import pkgutil
torch_loader = pkgutil.find_loader(module_name)
elif sys.version_info >= (3, 4):
# python 3.4 and above
import importlib
torch_loader = importlib.util.find_spec(module_name)
return torch_loader is not None
Here is a way to check if a module is loaded from the command line:
Linux/UNIX script file method: make a file module_help.py:
#!/usr/bin/env python
help('modules')
Then make sure it's executable: chmod u+x module_help.py
And call it with a pipe to grep:
./module_help.py | grep module_name
Invoke the built-in help system. (This function is intended for interactive use.) If no argument is given, the interactive help system starts on the interpreter console. If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.
Interactive method: in the console, load python
>>> help('module_name')
If found, quit reading by typing q.
To exit the Python interpreter interactive session, press Ctrl + D
Windows script file method, also Linux/UNIX compatible, and better overall:
#!/usr/bin/env python
import sys
help(sys.argv[1])
Calling it from the command like:
python module_help.py site
Would output:
Help on module site:
NAME
site - Append module search paths for third-party packages to sys.path.
FILE
/usr/lib/python2.7/site.py
MODULE DOCS
http://docs.python.org/library/site
DESCRIPTION
...
:
And you'd have to press q to exit interactive mode.
Using it for an unknown module, e.g.,
python module_help.py lkajshdflkahsodf
Would output:
no Python documentation found for 'lkajshdflkahsodf'
and exit.
You could just write a little script that would try to import all the modules and tell you which ones are failing and which ones are working:
import pip
if __name__ == '__main__':
for package in pip.get_installed_distributions():
pack_string = str(package).split(" ")[0]
try:
if __import__(pack_string.lower()):
print(pack_string + " loaded successfully")
except Exception as e:
print(pack_string + " failed with error code: {}".format(e))
Output:
zope.interface loaded successfully
zope.deprecation loaded successfully
yarg loaded successfully
xlrd loaded successfully
WMI loaded successfully
Werkzeug loaded successfully
WebOb loaded successfully
virtualenv loaded successfully
...
A word of warning: this will try to import everything, so you'll see things like PyYAML failed with error code: No module named pyyaml, because the actual import name is just yaml. So as long as you know your imports, this should do the trick for you.
There isn't any way to reliably check if "dotted module" is importable without importing its parent package. Saying this, there are many solutions to problem "how to check if a Python module exists".
The below solution addresses the problem that an imported module can raise an ImportError even if it exists. We want to distinguish that situation from such in which the module does not exist.
Python 2:
import importlib
import pkgutil
import sys
def find_module(full_module_name):
"""
Returns module object if module `full_module_name` can be imported.
Returns None if module does not exist.
Exception is raised if (existing) module raises exception during its import.
"""
module = sys.modules.get(full_module_name)
if module is None:
module_path_tail = full_module_name.split('.')
module_path_head = []
loader = True
while module_path_tail and loader:
module_path_head.append(module_path_tail.pop(0))
module_name = ".".join(module_path_head)
loader = bool(pkgutil.find_loader(module_name))
if not loader:
# Double check if module realy does not exist
# (case: full_module_name == 'paste.deploy')
try:
importlib.import_module(module_name)
except ImportError:
pass
else:
loader = True
if loader:
module = importlib.import_module(full_module_name)
return module
Python 3:
import importlib
def find_module(full_module_name):
"""
Returns module object if module `full_module_name` can be imported.
Returns None if module does not exist.
Exception is raised if (existing) module raises exception during its import.
"""
try:
return importlib.import_module(full_module_name)
except ImportError as exc:
if not (full_module_name + '.').startswith(exc.name + '.'):
raise
In django.utils.module_loading.module_has_submodule:
import sys
import os
import imp
def module_has_submodule(package, module_name):
"""
check module in package
django.utils.module_loading.module_has_submodule
"""
name = ".".join([package.__name__, module_name])
try:
# None indicates a cached miss; see mark_miss() in Python/import.c.
return sys.modules[name] is not None
except KeyError:
pass
try:
package_path = package.__path__ # No __path__, then not a package.
except AttributeError:
# Since the remainder of this function assumes that we're dealing with
# a package (module with a __path__), so if it's not, then bail here.
return False
for finder in sys.meta_path:
if finder.find_module(name, package_path):
return True
for entry in package_path:
try:
# Try the cached finder.
finder = sys.path_importer_cache[entry]
if finder is None:
# Implicit import machinery should be used.
try:
file_, _, _ = imp.find_module(module_name, [entry])
if file_:
file_.close()
return True
except ImportError:
continue
# Else see if the finder knows of a loader.
elif finder.find_module(name):
return True
else:
continue
except KeyError:
# No cached finder, so try and make one.
for hook in sys.path_hooks:
try:
finder = hook(entry)
# XXX Could cache in sys.path_importer_cache
if finder.find_module(name):
return True
else:
# Once a finder is found, stop the search.
break
except ImportError:
# Continue the search for a finder.
continue
else:
# No finder found.
# Try the implicit import machinery if searching a directory.
if os.path.isdir(entry):
try:
file_, _, _ = imp.find_module(module_name, [entry])
if file_:
file_.close()
return True
except ImportError:
pass
# XXX Could insert None or NullImporter
else:
# Exhausted the search, so the module cannot be found.
return False
In case you know the location of file and want to check that the respective Python code file has that module or not, you can simply check via the astor package in Python. Here is a quick example:
"""
Check if a module function exists or not without importing a Python package file
"""
import ast
import astor
tree = astor.parse_file('handler.py')
method_to_check = 'handle'
for item in tree.body:
if isinstance(item, ast.FunctionDef):
if item.name == method_to_check:
print('method exists')
break
A simpler if statement from Ask Ubuntu, How do I check whether a module is installed in Python?:
import sys
print('eggs' in sys.modules)
You can also use importlib.util directly
import importlib.util
def module_exists_without_import(module_name):
spec = importlib.util.find_spec(module_name)
return spec is not None

How to catch an ImportError non-recursively?

Say we want to import a script named user.py, which may fail.
try:
import user
except ImportError:
logging.info('No user script loaded.')
How can we make sure to only catch the possible import failure of user.py itself, and not of the imports that may be contained in user.py?
You could check to see if the current traceback is part of a chain of tracebacks:
import sys
try:
import user
except ImportError:
if sys.exc_info()[2].tb_next:
raise
logging.info('No user script loaded.')
If there is an ImportError in user, sys.exc_info()[2].tb_next will point to it.
You could look at the arguments:
try:
import user
except ImportError as exception:
if 'user' == exception.args[0][16:]:
logging.info('No user script loaded.')
This ensures you'll only log that message when the user script fails to be imported.
Although, it can be argued that failing to import one of the imports in user also implies that it can't import user (which means you'd need to log the message anyway).

Python 'import X as Y' results in 'NameError global name 'Y' is not defined

I'm new to python and trying to wrap my head around this error from the code below:
try:
import _winreg as winreg
except ImportError:
pass
...
path = 'HARDWARE\\DEVICEMAP\\SERIALCOMM'
try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path)
except WindowsError, e:
if e.errno == 2:
return []
else:
raise e
Outputs: NameError global name 'winreg' is not defined.
What am I missing to get this working? My guess is that they included 'import as' because _winreg is simply winreg in python 3+. I have tried simply importing as _winreg and replacing the winreg -> _winreg but that also returns a NameError with '_winreg' not defined.
Thanks in advance!
You're silencing the ImportError.
try:
import _winreg as winreg
except ImportError:
pass
winreg is most likely not getting imported here, hence the NameError: the winreg name was never assigned because import failed.
You could remove the try / except block to confirm what's happening.
Since you want to support Python 3, what you're most likely looking for is:
try:
import _winreg as winreg # Try importing on Python 2
except ImportError:
import winreg # Fallback to Python 3 (if this raises an Exception, it'll escalate)
(_winreg was renamed in Python 3)
Your code works on python 2.x but fails silently on python 3.x. The proper thing to do is to try the 3.x import and fall back to the 2.x import if that fails. Since the second import is not protected by a try/except block, it will fail if winreg doesn't exist in either form - for instance, if its run on a linux machine.
try:
# try python3 import
import winreg
except ImportError:
# fall back to python2 import
import _winreg as winreg
# test code proves it works
print(winreg)

Categories