10 examples of 'how to import module in pycharm' in Python

Every line of 'how to import module in pycharm' 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
686def import_module(pkg, module_name):
687 full_module_name = pkg.__name__ + "." + module_name
688 toplevel_module = __import__(full_module_name)
689 module = toplevel_module
690 for attr in full_module_name.split(".")[1:]:
691 module = getattr(module, attr)
692 return module
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

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
73def try_import_module(module_name):
74 """
75 Imports a module, but catches import errors. Only catches errors
76 when that module doesn't exist; if that module itself has an
77 import error it will still get raised. Returns None if the module
78 doesn't exist.
79 """
80 try:
81 return import_module(module_name)
82 except ImportError as e:
83 if not getattr(e, 'args', None):
84 raise
85 desc = e.args[0]
86 if not desc.startswith('No module named '):
87 raise
88 desc = desc[len('No module named '):]
89 # If you import foo.bar.baz, the bad import could be any
90 # of foo.bar.baz, bar.baz, or baz; we'll test them all:
91 parts = module_name.split('.')
92 for i in range(len(parts)):
93 if desc == '.'.join(parts[i:]):
94 return None
95 raise
50@staticmethod
51def import_module(module):
52 d = module.rfind(".")
53 modulename = module[d+1:len(module)]
54 mod = __import__(module, globals(), locals(), [modulename])
55 return mod
69def import_module(module_name):
70 '''
71 Imports the specified module by name.
72 '''
73
74 parts = module_name.split('.')
75 module = __import__(module_name)
76 for part in parts[1:]:
77 module = getattr(module, part)
78 return module
11def import_module(name):
12 __import__("%s" % (name,), globals(), locals(), [], -1)
13 return sys.modules[name]
28def import_module(name):
29 """
30 Imports a Python module assuming that the whole *name* identifies only a
31 Python module and no symbol inside a Python module.
32 """
33
34 # fromlist must not be empty so we get the bottom-level module rather than
35 # the top-level module.
36 return __import__(name, fromlist=[''])
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()
35def import_module(module_name):
36 module = __import__(module_name)
37 if '.' in module_name:
38 return reduce(getattr, module_name.split('.')[1:], module)
39 return module
680def _import_module(module):
681 __import__(module)
682 return sys.modules[module]

Related snippets