Add current working directory, '', to sys.path Unless disabled by ignore_cwd config or sys.flags.safe_path. Unlike Python's default, we insert before the first `site-packages` or `dist-packages` directory, so that it is after the standard library. .. versio
(self)
| 245 | self.shell.init_user_ns() |
| 246 | |
| 247 | def init_path(self): |
| 248 | """Add current working directory, '', to sys.path |
| 249 | |
| 250 | Unless disabled by ignore_cwd config or sys.flags.safe_path. |
| 251 | |
| 252 | Unlike Python's default, we insert before the first `site-packages` |
| 253 | or `dist-packages` directory, |
| 254 | so that it is after the standard library. |
| 255 | |
| 256 | .. versionchanged:: 7.2 |
| 257 | Try to insert after the standard library, instead of first. |
| 258 | .. versionchanged:: 8.0 |
| 259 | Allow optionally not including the current directory in sys.path |
| 260 | .. versionchanged:: 9.7 |
| 261 | Respect sys.flags.safe_path (PYTHONSAFEPATH and -P flag) |
| 262 | """ |
| 263 | if "" in sys.path or self.ignore_cwd or sys.flags.safe_path: |
| 264 | return |
| 265 | for idx, path in enumerate(sys.path): |
| 266 | parent, last_part = os.path.split(path) |
| 267 | if last_part in {'site-packages', 'dist-packages'}: |
| 268 | break |
| 269 | else: |
| 270 | # no site-packages or dist-packages found (?!) |
| 271 | # back to original behavior of inserting at the front |
| 272 | idx = 0 |
| 273 | sys.path.insert(idx, '') |
| 274 | |
| 275 | def init_shell(self): |
| 276 | raise NotImplementedError("Override in subclasses") |