Skip to content

Commit 24d3cf8

Browse files
committed
[GR-8143] Support PYTHONPATH environment variable for prefixing sys.path
1 parent 4294544 commit 24d3cf8

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ protected void launch(Builder contextBuilder) {
172172
contextBuilder.option("log.python.level", "FINE");
173173
}
174174

175+
String pythonpath = System.getenv("PYTHONPATH");
176+
if (pythonpath != null) {
177+
contextBuilder.option("python.PythonPath", pythonpath);
178+
}
179+
175180
ConsoleHandler consoleHandler = createConsoleHandler(System.in, System.out);
176181
contextBuilder.arguments(getLanguageId(), programArgs.toArray(new String[0])).in(consoleHandler.createInputStream());
177182

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
import com.oracle.graal.python.builtins.objects.type.TypeBuiltins;
131131
import com.oracle.graal.python.runtime.PythonContext;
132132
import com.oracle.graal.python.runtime.PythonCore;
133+
import com.oracle.graal.python.runtime.PythonOptions;
133134
import com.oracle.graal.python.runtime.PythonParser;
134135
import com.oracle.graal.python.runtime.exception.PException;
135136
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -364,19 +365,23 @@ public PythonModule initializeSysModule() {
364365

365366
private void initializeSysPath(PythonModule sys, String[] args) {
366367
Env env = getContext().getEnv();
367-
Object[] path = new Object[]{
368-
getScriptPath(env, args),
369-
PythonCore.getStdlibHome(env),
370-
PythonCore.getCoreHome(env) + PythonCore.FILE_SEPARATOR + "modules"};
368+
String option = PythonOptions.getOption(getContext(), PythonOptions.PythonPath);
369+
Object[] path;
370+
int pathIdx = 0;
371+
if (option.length() > 0) {
372+
String[] split = option.split(PythonCore.PATH_SEPARATOR);
373+
path = new Object[split.length + 3];
374+
System.arraycopy(split, 0, path, 0, split.length);
375+
pathIdx = split.length;
376+
} else {
377+
path = new Object[3];
378+
}
379+
path[pathIdx] = getScriptPath(env, args);
380+
path[pathIdx + 1] = PythonCore.getStdlibHome(env);
381+
path[pathIdx + 2] = PythonCore.getCoreHome(env) + PythonCore.FILE_SEPARATOR + "modules";
371382
PList sysPaths = factory().createList(path);
372383
sys.setAttribute("path", sysPaths);
373384
// sysPaths.append(getPythonLibraryExtrasPath());
374-
String pythonPath = System.getenv("PYTHONPATH");
375-
if (pythonPath != null) {
376-
for (String s : pythonPath.split(PythonCore.PATH_SEPARATOR)) {
377-
sysPaths.append(s);
378-
}
379-
}
380385
}
381386

382387
private static String getScriptPath(Env env, String[] args) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ private PythonOptions() {
8585
@Option(category = OptionCategory.USER, help = "") //
8686
public static final OptionKey<Boolean> InspectFlag = new OptionKey<>(false);
8787

88+
@Option(category = OptionCategory.USER, help = "") //
89+
public static final OptionKey<String> PythonPath = new OptionKey<>("");
90+
8891
@Option(category = OptionCategory.USER, help = "Remove assert statements and any code conditional on the value of __debug__.") //
8992
public static final OptionKey<Boolean> PythonOptimizeFlag = new OptionKey<>(false);
9093

0 commit comments

Comments
 (0)