Skip to content

Commit 8c21493

Browse files
authored
Fix native REPL search path (#25372)
Resolves: #24361
1 parent 4e0633b commit 8c21493

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

python_files/python_server.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import traceback
77
import uuid
8+
from pathlib import Path
89
from typing import Dict, List, Optional, Union
910

1011
STDIN = sys.stdin
@@ -172,6 +173,16 @@ def get_headers():
172173

173174

174175
if __name__ == "__main__":
176+
# https://docs.python.org/3/tutorial/modules.html#the-module-search-path
177+
# The directory containing the input script (or the current directory when no file is specified).
178+
# Here we emulate the same behavior like no file is specified.
179+
input_script_dir = Path(__file__).parent
180+
script_dir_str = str(input_script_dir)
181+
if script_dir_str in sys.path:
182+
sys.path.remove(script_dir_str)
183+
while "" in sys.path:
184+
sys.path.remove("")
185+
sys.path.insert(0, "")
175186
while not STDIN.closed:
176187
try:
177188
headers = get_headers()

0 commit comments

Comments
 (0)