diff --git a/runtime/src/jycessing/Runner.java b/runtime/src/jycessing/Runner.java index 08016fe1..67274515 100644 --- a/runtime/src/jycessing/Runner.java +++ b/runtime/src/jycessing/Runner.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Properties; import java.util.Set; +import java.util.TreeSet; import org.python.core.Py; import org.python.core.PyList; @@ -295,15 +296,12 @@ public static synchronized void runSketchBlocking( final StringBuilder pythonPath = new StringBuilder(); - final List libDirs = sketch.getLibraryDirectories(); - // The code may be located in a temp dir, if the sketch is unsaved. final String actualCodeLocation = sketch.getMainFile().getParentFile().getAbsolutePath(); pythonPath.append(File.pathSeparator).append(actualCodeLocation); final String sketchDirPath = sketch.getHomeDirectory().getAbsolutePath(); pythonPath.append(File.pathSeparator).append(sketchDirPath); - props.setProperty("python.path", pythonPath.toString()); props.setProperty("python.main", sketch.getMainFile().getAbsolutePath()); props.setProperty("python.main.root", sketchDirPath); @@ -326,15 +324,16 @@ public static synchronized void runSketchBlocking( // Add all of the sketch's requested sys.path entries, and add all jar // files found there, recursively. - final Set userLibs = new HashSet<>(); + final Set userLibs = new TreeSet<>(); for (final File entry : sketch.getPathEntries()) { - sys.path.append(Py.newString(entry.getAbsolutePath())); + userLibs.add(entry.getAbsolutePath()); searchForExtraStuff(entry, userLibs); } // Add the add_library function to the sketch namespace. + final List libDirs = sketch.getLibraryDirectories(); if (libDirs != null) { - new LibraryImporter(sketch.getLibraryDirectories(), interp); + new LibraryImporter(libDirs, interp); if (sketch.getLibraryPolicy() == LibraryPolicy.PROMISCUOUS) { log("Promiscusouly adding all libraries in " + libDirs); @@ -345,7 +344,7 @@ public static synchronized void runSketchBlocking( searchForExtraStuff(dir, libs); } for (final String lib : libs) { - sys.path.insert(0, Py.newString(lib)); + userLibs.add(lib); } } } diff --git a/runtime/src/jycessing/mode/PythonMode.java b/runtime/src/jycessing/mode/PythonMode.java index 5ce8e4d2..c994983a 100644 --- a/runtime/src/jycessing/mode/PythonMode.java +++ b/runtime/src/jycessing/mode/PythonMode.java @@ -32,6 +32,8 @@ public class PythonMode extends Mode { public static final boolean SKETCH_RUNNER_FIRST = Boolean.parseBoolean(System.getenv("SKETCH_RUNNER_FIRST")); + public static final String SITE_PACKAGES = "site-packages"; + /** * Python auto-formatting is handled by a server. {@link FormatServer} handles the lifecycle of, * and communication with, that server. @@ -56,6 +58,20 @@ public PythonMode(final Base base, final File folder) { * and not on any API. May break in the future. */ librariesFolder = Platform.getContentFile("modes/java/libraries"); + final File pythonLibs = getSitePackages(); + if (pythonLibs.exists()) { + if (!pythonLibs.isDirectory()) { + System.err.println(pythonLibs + " exists but is not directory"); + } + } else { + if (!pythonLibs.mkdirs()) { + System.err.println("cannot create " + pythonLibs); + } + } + } + + public static File getSitePackages() { + return new File(Base.getSketchbookLibrariesFolder(), SITE_PACKAGES); } @Override @@ -76,7 +92,7 @@ public Editor createEditor(final Base base, final String path, final EditorState try { return new PyEditor(base, path, state, this); - } catch (EditorException e) { + } catch (final EditorException e) { Messages.showError("Editor Exception", "Issue Creating Editor", e); return null; } diff --git a/runtime/src/jycessing/mode/run/PdeSketch.java b/runtime/src/jycessing/mode/run/PdeSketch.java index 507078d0..92bce2a1 100644 --- a/runtime/src/jycessing/mode/run/PdeSketch.java +++ b/runtime/src/jycessing/mode/run/PdeSketch.java @@ -4,11 +4,13 @@ import java.io.File; import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import jycessing.DisplayType; import jycessing.RunnableSketch; import jycessing.Runner.LibraryPolicy; +import jycessing.mode.PythonMode; import processing.app.Base; import processing.app.Platform; import processing.app.Sketch; @@ -23,6 +25,7 @@ public class PdeSketch implements RunnableSketch, Serializable { private final List libraryDirs; + private final List pythonLibraryDirs; private final File mainFile; private final String mainCode; private final File sketchHome; @@ -60,6 +63,8 @@ public PdeSketch( codeFileNames[i] = sketch.getCode(i).getFile().getName(); } this.codeFileNames = codeFileNames; + this.pythonLibraryDirs = + Arrays.asList(PythonMode.getSitePackages().getAbsoluteFile()); } public static enum LocationType { @@ -132,6 +137,7 @@ public List getPathEntries() { if (code.exists()) { entries.add(code); } + entries.addAll(pythonLibraryDirs); return entries; } }