forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_tools.py
More file actions
73 lines (52 loc) · 1.98 KB
/
import_tools.py
File metadata and controls
73 lines (52 loc) · 1.98 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
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
'''Defines various tools related to importing.'''
import sys
import os.path
import importlib
import zipimport
import functools
import pathlib
from python_toolbox import caching
def normal_import(module_name):
'''
Import a module.
This function has several advantages over `__import__`:
1. It avoids the weird `fromlist=['']` that you need to give `__import__`
in order for it to return the specific module you requested instead of
the outermost package, and
2. It avoids a weird bug in Linux, where importing using `__import__` can
lead to a `module.__name__` containing two consecutive dots.
'''
if '.' in module_name:
package_name, submodule_name = module_name.rsplit('.', 1)
package = __import__(module_name)
return functools.reduce(getattr,
[package] + module_name.split('.')[1:])
else:
return __import__(module_name)
def import_if_exists(module_name):
'''
Import module by name and return it, only if it exists.
'''
try:
return __import__(module_name)
except ModuleNotFoundError:
return None
def exists(module_name, package_name=None):
'''
Return whether a module by the name `module_name` exists.
This seems to be the best way to carefully import a module.
Currently implemented for top-level packages only. (i.e. no dots.)
Supports modules imported from a zip file.
'''
if '.' in module_name:
raise NotImplementedError
return bool(importlib.util.find_spec(module_name, package_name))
def _module_address_to_partial_path(module_address):
'''
Convert a dot-seperated address to a path-seperated address.
For example, on Linux, `'python_toolbox.caching.cached_property'` would be
converted to `'python_toolbox/caching/cached_property'`.
'''
return os.path.sep.join(module_address.split('.'))