-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathipython_config.py
36 lines (28 loc) · 1.27 KB
/
ipython_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import subprocess
from pathlib import Path
from typing import Union
####################################################################################################
# Additional PYTHONPATH to allow notebooks to import custom modules at a few pre-defined places.
def sys_path_append(o: Union[str, os.PathLike]) -> str:
posix_path: str = o.as_posix() if isinstance(o, Path) else Path(o).as_posix()
return 'sys.path.append("{}")'.format(posix_path)
_pythonpath = [
"import sys, os",
sys_path_append(os.getcwd()),
]
# Add GIT_ROOT/ and a few other subdirs
try:
_p = subprocess.run(["git", "rev-parse", "--show-toplevel"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if _p.returncode == 0:
_git_root: str = _p.stdout[:-1].decode("utf-8") # Remove trailing '\n'
_git_root_p: Path = Path(_git_root)
_pythonpath += [
sys_path_append(_git_root_p), # GIT_ROOT
sys_path_append(_git_root_p / "src"), # GIT_ROOT/src
sys_path_append(_git_root_p / "notebooks"), # GIT_ROOT/notebooks
]
except: # noqa: E722
pass
c.InteractiveShellApp.exec_lines = _pythonpath # type: ignore # noqa: F821
####################################################################################################