Return path as an imported python module. If modname is None, look for the containing package and construct an according module name. The module will be put/looked up in sys.modules. if ensuresyspath is True then the root dir for importing the file (taking __
(self, modname=None, ensuresyspath=True)
| 1068 | sys.path.insert(0, s) |
| 1069 | |
| 1070 | def pyimport(self, modname=None, ensuresyspath=True): |
| 1071 | class="st">"""Return path as an imported python module. |
| 1072 | |
| 1073 | If modname is None, look for the containing package |
| 1074 | and construct an according module name. |
| 1075 | The module will be put/looked up in sys.modules. |
| 1076 | if ensuresyspath is True then the root dir for importing |
| 1077 | the file (taking __init__.py files into account) will |
| 1078 | be prepended to sys.path if it isn&class="cm">#x27;t there already. |
| 1079 | If ensuresyspath==class="st">"append" the root dir will be appended |
| 1080 | if it isn&class="cm">#x27;t already contained in sys.path. |
| 1081 | if ensuresyspath is False no modification of syspath happens. |
| 1082 | |
| 1083 | Special value of ensuresyspath==class="st">"importlib" is intended |
| 1084 | purely for using in pytest, it is capable only of importing |
| 1085 | separate .py files outside packages, e.g. for test suite |
| 1086 | without any __init__.py file. It effectively allows having |
| 1087 | same-named test modules in different places and offers |
| 1088 | mild opt-in via this option. Note that it works only in |
| 1089 | recent versions of python. |
| 1090 | class="st">""" |
| 1091 | if not self.check(): |
| 1092 | raise error.ENOENT(self) |
| 1093 | |
| 1094 | if ensuresyspath == class="st">"importlib": |
| 1095 | if modname is None: |
| 1096 | modname = self.purebasename |
| 1097 | spec = importlib.util.spec_from_file_location(modname, str(self)) |
| 1098 | if spec is None or spec.loader is None: |
| 1099 | raise ImportError(fclass="st">"Can&class="cm">#x27;t find module {modname} at location {self!s}") |
| 1100 | mod = importlib.util.module_from_spec(spec) |
| 1101 | spec.loader.exec_module(mod) |
| 1102 | return mod |
| 1103 | |
| 1104 | pkgpath = None |
| 1105 | if modname is None: |
| 1106 | pkgpath = self.pypkgpath() |
| 1107 | if pkgpath is not None: |
| 1108 | pkgroot = pkgpath.dirpath() |
| 1109 | names = self.new(ext=class="st">"").relto(pkgroot).split(self.sep) |
| 1110 | if names[-1] == class="st">"__init__": |
| 1111 | names.pop() |
| 1112 | modname = class="st">".".join(names) |
| 1113 | else: |
| 1114 | pkgroot = self.dirpath() |
| 1115 | modname = self.purebasename |
| 1116 | |
| 1117 | self._ensuresyspath(ensuresyspath, pkgroot) |
| 1118 | __import__(modname) |
| 1119 | mod = sys.modules[modname] |
| 1120 | if self.basename == class="st">"__init__.py": |
| 1121 | return mod class="cm"># we don't check anything as we might |
| 1122 | class="cm"># be in a namespace package ... too icky to check |
| 1123 | modfile = mod.__file__ |
| 1124 | assert modfile is not None |
| 1125 | if modfile[-4:] in (class="st">".pyc", class="st">".pyo"): |
| 1126 | modfile = modfile[:-1] |
| 1127 | elif modfile.endswith(class="st">"$py.class"): |