10 examples of 'python import variable from another file' in Python

Every line of 'python import variable from another file' 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
this disclaimer
91def visit_ImportFrom(self, node):
92 if node.module != '__future__':
93 for alias in node.names:
94 if alias.name != '*':
95 alias.asname = self.get_new_name(alias.asname, is_imported=True)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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()
234def visit_ImportFrom(self, node):
235 self.warn('non-global-import', node)
324def test_import_py_nested1_mod_func(self, php_space):
325 output = self.run('''
326 $os_path = import_py_mod("os.path");
327 echo($os_path->join("a", "b"));
328 ''')
329 assert php_space.str_w(output[0]) == "a/b"
22def visit_Import(self, node):
23 """
24 This executes every time an "import foo" style import statement
25 is parsed.
26 """
27 for n in node.names:
28 self.imports.append((n.name, n.asname, None))
27def my_import(name):
28 """ dynamic importing """
29 module, attr = name.rsplit('.', 1)
30 mod = __import__(module, fromlist=[attr])
31 klass = getattr(mod, attr)
32 return klass()
115def visit_import(self, node):
116 """
117 Append node names to ``self.modules``.
118
119 :param node: an ``ast`` object representing a python statement.
120
121 .. versionadded:: 0.1.0
122 """
123 self.modules.extend((n.name, 0) for n in node.names)
23def import_modules(source_code: str):
24 results = {}
25 imports = parse_imports(source_code)
26 for import_ in imports:
27 module = import_.module if import_.module else import_.name
28 loaded_module = importlib.import_module(module)
29
30 if not import_.name == module:
31 loaded_module = getattr(loaded_module, import_.name)
32
33 if import_.alias:
34 results[import_.alias] = loaded_module
35 else:
36 results[import_.name] = loaded_module
37
38 return results
38def load_module(self, file=None):
39 if file is None:
40 self._module_co = None
41
42 logging.basicConfig(level=logging.DEBUG)
43 try:
44 self._module_co = imp.load_source('ifigure.add_on.tmp', file)
45 name = self._module_co.module_name
46 self._module_co = imp.load_source('ifigure.add_on'+name, file)
47 self._module_mtime = os.path.getmtime(file)
48 self._module_file = file
49 self._func = self._module_co.func
50 self._func_init = self._module_co.func_init
51 self._method_init = self._module_co.method_init
52 self._menu = self._module_co.menu
53 for mname in self._module_co.method:
54 m = getattr(self._module_co, mname)
55 object(TreeDict, self).__setattr__(self, mname,
56 m.__get__(self, self, self.__class__))
57 except Exception:
58 logging.exception("Module Loading Failed")
6def test_basic_import_self(self, tmpdir):
7 main_file = tmpdir.join('main.q')
8 main_file.write(reformat_code('''
9 import self.foo.bar
10 import self.foo
11
12 def main() {
13 print(bar(3) + foo.bar(13))
14 }
15 '''))
16
17 foo_file = tmpdir.join('foo.q')
18 foo_file.write(reformat_code('''
19 def bar(i) {
20 return i + 3
21 }
22 '''))
23 run_code(str(main_file))

Related snippets