10 examples of 'python import from parent directory' in Python

Every line of 'python import from parent directory' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
4def import_dir(name, fromlist=()):
5 PACKAGE_EXT = '.sublime-package'
6 dirname = os.path.basename(os.path.dirname(os.path.realpath(__file__)))
7 if dirname.endswith(PACKAGE_EXT):
8 dirname = dirname[:-len(PACKAGE_EXT)]
9 return __import__('{0}.{1}'.format(dirname, name), fromlist=fromlist)
4def import_path(path):
5 path = os.path.abspath(path)
6 path, file = os.path.split(path)
7 file, ext = os.path.splitext(file)
8 sys.path.append(path)
9 module = __import__(file)
10 sys.path.pop()
11 return module
69def _import_from(mod, path, mod_dir=None):
70 """
71 Imports a module from a specific path
72
73 :param mod:
74 A unicode string of the module name
75
76 :param path:
77 A unicode string to the directory containing the module
78
79 :param mod_dir:
80 If the sub directory of "path" is different than the "mod" name,
81 pass the sub directory as a unicode string
82
83 :return:
84 None if not loaded, otherwise the module
85 """
86
87 if mod_dir is None:
88 mod_dir = mod
89
90 if not os.path.exists(path):
91 return None
92
93 if not os.path.exists(os.path.join(path, mod_dir)):
94 return None
95
96 try:
97 mod_info = imp.find_module(mod_dir, [path])
98 return imp.load_module(mod, *mod_info)
99 except ImportError:
100 return None
90def __import__(name, globals=None, locals=None, fromlist=None):
91 """An alternative to the import function so that we can import
92 modules defined as strings.
93
94 This code was taken from: http://docs.python.org/lib/examples-imp.html
95 """
96 # Fast path: see if the module has already been imported.
97 try:
98 return sys.modules[name]
99 except KeyError:
100 pass
101
102 # If any of the following calls raises an exception,
103 # there's a problem we can't handle -- let the caller handle it.
104 module_name = name.split('.')[-1]
105 module_path = os.path.join(EXAMPLE_DIR, *name.split('.')[:-1])
106
107 fp, pathname, description = imp.find_module(module_name, [module_path])
108
109 try:
110 return imp.load_module(module_name, fp, pathname, description)
111 finally:
112 # Since we may exit via an exception, close fp explicitly.
113 if fp:
114 fp.close()
51def import_modules_from_package(package):
52 """Import modules from package and append into sys.modules
53
54 :param package: Full package name. For example: rally.plugins.openstack
55 """
56 path = [os.path.dirname(rally.__file__), ".."] + package.split(".")
57 path = os.path.join(*path)
58 for root, dirs, files in os.walk(path):
59 for filename in files:
60 if filename.startswith("__") or not filename.endswith(".py"):
61 continue
62 new_package = ".".join(root.split(os.sep)).split("....")[1]
63 module_name = "%s.%s" % (new_package, filename[:-3])
64 if module_name not in sys.modules:
65 sys.modules[module_name] = importlib.import_module(module_name)
111def load_external_module(base: str, module: str) -> None:
112 current = os.path.dirname(os.path.abspath(base))
113 module_dir = join(os.path.dirname(current), module)
114 sys.path.insert(0, module_dir)
27def __import__(self, name, globals=None, locals=None, fromlist=(), level=0): # noqa
28 module = self._orig___import__(name, globals, locals, fromlist, level)
29
30 self.reload(module)
31
32 if fromlist:
33 from_names = [
34 name
35 for item in fromlist
36 for name in (
37 getattr(module, '__all__', []) if item == '*' else (item,)
38 )
39 ]
40
41 for name in from_names:
42 value = getattr(module, name, None)
43 if ismodule(value):
44 self.reload(value)
45
46 return module
99def __init__(self, module, extra_dir=None):
100 """
101 :param module: str
102 module spec for import or file path
103 from that only basename without .py is used
104 :param extra_dir: str or None
105 extra dir to prepend to sys.path
106 if module then doesn't change sys.path if None
107 if file then prepends dir if None
108 """
109 def remove_py(s):
110 return s[:-3] if s.endswith('.py') else s
111
112 self.module = remove_py(p.basename(module))
113 if (extra_dir is None) and (module != p.basename(module)):
114 extra_dir = p.dirname(module)
115 self.extra_dir = extra_dir
70def __init__(self, path=None, name=None, sys_path=None):
71 if sys_path is None:
72 sys_path = modules.get_sys_path()
73 if not name:
74 name = os.path.basename(path)
75 name = name.rpartition('.')[0] # cut file type (normally .so)
76 super(BuiltinModule, self).__init__(path=path, name=name)
77
78 self.sys_path = list(sys_path)
79 self._module = None
56def _import_module(importer, module_name, package):
57 """Import a module dynamically into a package.
58
59 :param importer: PEP302 Importer object (which knows the path to look in).
60 :param module_name: the name of the module to import.
61 :param package: the package to import the module into.
62 """
63
64 # Duplicate copies of modules are bad, so check if this has already been
65 # imported statically
66 if module_name in sys.modules:
67 return sys.modules[module_name]
68
69 loader = importer.find_module(module_name)
70 if loader is None:
71 return None
72
73 module = loader.load_module(module_name)
74
75 # Make this accessible through the parent package for static imports
76 local_name = module_name.partition(package.__name__ + '.')[2]
77 module_components = local_name.split('.')
78 parent = functools.reduce(getattr, module_components[:-1], package)
79 setattr(parent, module_components[-1], module)
80
81 return module

Related snippets