Issue2355
Created on 2015-05-12.09:27:54 by otto_fajardo, last changed 2018-04-27.06:56:26 by angel.
Messages | |||
---|---|---|---|
msg10053 (view) | Author: Otto Fajardo (otto_fajardo) | Date: 2015-05-12.09:27:53 | |
When embedding Jython 2.7.0 into an eclipse plugin, it is not possible to import python modules from Lib, as site, os or heapq. Modules like sys import OK. Site import can be skipped with python.import.site set to false, but that does not help importing os for example. The python.home and sys.path look correct, Lib folder is in the path, the modules are in the folder, and still they fail to import. This behavior is not present if I use jython 2.5.3. How to reproduce it: The scaffold is taken from the tutorials on how to do eclipse plugins. As described here: http://www.vogella.com/tutorials/EclipsePlugIn/article.html The samplehandler class is the one initializing jython, and the one that generates the error public class SampleHandler extends AbstractHandler { /** * The constructor. */ public SampleHandler() { } /** * the command has been executed, so extract extract the needed information * from the application context. */ public Object execute(ExecutionEvent event) throws ExecutionException { //here create the jython etc. long startime = System.nanoTime(); Properties props = new Properties(); //if we do the next line, then it works, but os import fails //props.put("python.import.site","false"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interp = new PythonInterpreter(); long midtime = System.nanoTime(); interp.exec("import sys"); interp.exec("print 'prefix', sys.prefix"); interp.exec("print sys.path"); interp.exec("import os"); System.out.println("so far so good"); //interp.exec("import subprocess"); long endtime = System.nanoTime(); long totaltime = (endtime-startime); long semitime = midtime -startime; System.out.println(String.valueOf(semitime)); System.out.println(String.valueOf(totaltime)); IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); MessageDialog.openInformation( window.getShell(), "Eclipse_plugin_test", "Hello, Eclipse world"); return null; } } ERROR MESSAGE: !ENTRY org.eclipse.ui 4 0 2015-05-07 21:36:15.461 !MESSAGE Unhandled event loop exception !STACK 0 org.eclipse.e4.core.di.InjectionException: ImportError: Cannot import site module and its dependencies: No module named site Determine if the following attributes are correct: * sys.path: ['C:\\devhome\\eclipse_kepler\\lib\\jython.jar\\Lib', '__classpath__', '__pyclasspath__/'] This attribute might be including the wrong directories, such as from CPython * sys.prefix: C:\devhome\eclipse_kepler\lib\jython.jar This attribute is set by the system property python.home, although it can be often automatically determined by the location of the Jython jar file You can use the -S option or python.import.site=false to not import the site module at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:63) at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:243) at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:224) at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132) at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:167) at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499) at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508) at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:213) at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:850) at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(HandledContributionItem.java:743) at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$7(HandledContributionItem.java:727) at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(HandledContributionItem.java:662) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997) at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:140) at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:611) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591) at org.eclipse.equinox.launcher.Main.run(Main.java:1450) at org.eclipse.equinox.launcher.Main.main(Main.java:1426) Caused by: ImportError: Cannot import site module and its dependencies: No module named site Determine if the following attributes are correct: * sys.path: ['C:\\devhome\\eclipse_kepler\\lib\\jython.jar\\Lib', '__classpath__', '__pyclasspath__/'] This attribute might be including the wrong directories, such as from CPython * sys.prefix: C:\devhome\eclipse_kepler\lib\jython.jar This attribute is set by the system property python.home, although it can be often automatically determined by the location of the Jython jar file You can use the -S option or python.import.site=false to not import the site module at org.python.core.Py.ImportError(Py.java:328) at org.python.core.Py.importSiteIfSelected(Py.java:1563) at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:116) at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:94) at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:71) at eclipse_plugin_test.handlers.SampleHandler.execute(SampleHandler.java:54) at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:290) at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56) ... 37 more |
|||
msg10054 (view) | Author: Otto Fajardo (otto_fajardo) | Date: 2015-05-12.09:32:23 | |
Forgot to mention: I run eclipse kepler, java 7, on windows 7 |
|||
msg10055 (view) | Author: Otto Fajardo (otto_fajardo) | Date: 2015-05-12.10:26:49 | |
and yet another point: I use the standalone version of jython, which I put inside a folder in the eclipse project |
|||
msg10057 (view) | Author: Otto Fajardo (otto_fajardo) | Date: 2015-05-12.12:26:33 | |
By a suggestion of mbooth, I looked into PyDev and how they embed jython into an eclipse plugin. It seems they explode the jython.jar, and then copy the Lib folder into the top folder of the plugin. Then they set python.home to this top folder of the plugin. I tried this, and in this way jython can import the modules. So, this workaround solves the problem. I am not sure if this should be the best way, or jython should be able to find the modules inside the jar as it did in 2.5.3. |
|||
msg10149 (view) | Author: Soha (Soha) | Date: 2015-07-08.22:31:49 | |
Hi Otto I have the same issue with using jython in my java application. Could you explain more clearly the solution you found (step-by-step)? Thanks |
|||
msg10150 (view) | Author: Otto Fajardo (otto_fajardo) | Date: 2015-07-12.07:41:19 | |
hi Soha, I did like this. If you find a better way please share it. 1- Explode the jython jar: get the jython standalone jar, change the .jar to .zip, unzip it into a folder. Inside that unzipped folder you will find a subfolder Lib. 2- Copy that folder Lib into your eclipse project. 3- In you java code, when you initialize jython, point to that Lib folder as python.home, for example: Properties props = new Properties(); props.put("python.home","path to the Lib folder"); props.put("python.console.encoding", "UTF-8"); // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0. props.put("python.security.respectJavaAccessibility", "false"); //don't respect java accessibility, so that we can access protected members on subclasses props.put("python.import.site","false"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interp = new PythonInterpreter(); That's it! now it should work! |
|||
msg11923 (view) | Author: angel (angel) | Date: 2018-04-27.06:56:25 | |
Another way to solve this is instead of using jython.jar, using jython-standalone.jar. In Maven there is https://mvnrepository.com/artifact/org.python/jython-standalone eg pom.xml: <dependencies> <dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.1</version> </dependency> </dependencies> Works for me. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2018-04-27 06:56:26 | angel | set | nosy:
+ angel messages: + msg11923 |
2015-07-12 07:41:21 | otto_fajardo | set | messages: + msg10150 |
2015-07-08 22:31:49 | Soha | set | nosy:
+ Soha messages: + msg10149 |
2015-05-12 12:26:33 | otto_fajardo | set | messages: + msg10057 |
2015-05-12 10:26:49 | otto_fajardo | set | messages: + msg10055 |
2015-05-12 09:32:23 | otto_fajardo | set | messages: + msg10054 |
2015-05-12 09:27:54 | otto_fajardo | create |
Supported by Python Software Foundation,
Powered by Roundup