Issue2850

classification
Title: Exception while creating python interpreter
Type: behaviour Severity: urgent
Components: Library Versions: Jython 2.7.1
Milestone: Jython 2.7.1
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Vikhe, jeff.allen
Priority: Keywords:

Created on 2019-12-18.08:10:36 by Vikhe, last changed 2019-12-24.19:59:02 by jeff.allen.

Messages
msg12852 (view) Author: yogita (Vikhe) Date: 2019-12-18.08:10:36
Hi Team,

I am using Jpython standalone jar to execute webapp on benstalk But its giving me error :

Exception while creating python interpreter: ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
  * sys.path: [/var/app/current/BOOT-INF/lib/jython-standalone-2.7.1.jar/Lib, __classpath__, __pyclasspath__/]
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: /var/app/current/BOOT-INF/lib/jython-standalone-2.7.1.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
:
Please find my code :

PythonInterpreter pythonInterp = null;

		PyObject x = null;



		

	    try{

	            Properties p = new Properties();

	            p.setProperty("python.path", "/lib/jython-standalone-2.7.1.jar");
PythonInterpreter.initialize(System.getProperties(), p, new String[] {});

	            pythonInterp = new PythonInterpreter();

	    }catch(Exception ex){

	        System.out.println("Exception while creating python interpreter: "+ex.toString());

	        ex.printStackTrace();

	    }

		System.out.println("Initialed PythonInterpreter in... ");

		

		pythonInterp.exec("print 'Hello Yogita '");

I check by benstalk app .the Jar is at BOOT-INF/lib/jython-standalone-2.7.1.jar &
msg12896 (view) Author: Jeff Allen (jeff.allen) Date: 2019-12-24.19:59:01
Happy Christmas Yogita. I don't know Beanstalk, but your program doesn't reference it and isn't (AFAICT) a servlet. #2844 may be worth a look.

I do not think it is necessary to manipulate python.path as you do, and in any case, it should point to where additional Python code can be found, not the standalone JAR. Try without and see what sys.path ends up as.

However, I'm not getting the same result (Windows 10). With this program:

// Start an interpreter and check the sys.path

import org.python.util.PythonInterpreter;
import java.util.Properties;

public class Issue2850 {

    public static void main(String args[]) {
        Properties p = new Properties();
        //p.setProperty("python.path", "inst\jython-standalone-2.7.1.jar");
        PythonInterpreter.initialize(System.getProperties(), p, new String[] {});
        PythonInterpreter pythonInterp = new PythonInterpreter();
        pythonInterp.exec("import sys");
        pythonInterp.exec("print sys.path");
    }
}

and the JAR at inst\\jython-standalone-2.7.1.jar I still end up with a sensible sys.path:

PS issue2850> javac -cp "inst\jython-standalone-2.7.1.jar" Issue2850.java
PS issue2850> java -cp ".;inst\jython-standalone-2.7.1.jar" Issue2850
['...\\issue2850\\inst\\Lib', '...\\issue2850\\inst\\jython-standalone-2.7.1.jar\\Lib', '__classpath__', '__pyclasspath__/', '...\\.local\\lib\\jython2.7\\site-packages']

(That last one is in my home directory, but is empty.) Notice Jython looks for a Lib directory next to the JAR (at inst/Lib), as well as inside it. Your path is relatively short but still contains the place I'd expect to find site.py.
History
Date User Action Args
2019-12-24 19:59:02jeff.allensetnosy: + jeff.allen
messages: + msg12896
2019-12-18 08:10:36Vikhecreate