3

I am trying to execute a python file from netbeans using jython in a java program. My code is like this:

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), 
 new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("as1.py");

error is:

Traceback (most recent call last):
 File "as1.py", line 2, in <module>
 import datetime
ImportError: No module named datetime

and also interdependent python files also not importing those are in same directory.

like:

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("calen.py");

python files are:

calen.py:

from as1 import * 
print ( "I am printing" + str(Moh(1000).run()))

as1.py:

from time import time
import datetime
class Moh:
 def __init__(self, n): 
 self.n = n
 def run(self):
 data = [1,2,3,4,5]
 start = time()
 for i in range(self.n):
 data.append(i)
 end = time()
 return ( end - start )/self.n
if __name__ == "__main__":
 print ( "I am printing" + str(Moh(1000).run()))

error is:

Traceback (most recent call last):
 File "calen.py", line 1, in <module>
 from as1 import * 
ImportError: No module named as1
Richa
3,2992 gold badges30 silver badges51 bronze badges
asked Aug 20, 2014 at 14:04
3
  • what version of jython are you using? Commented Aug 20, 2014 at 14:38
  • Thanks for asking - Jyhton version 2.5.3 Commented Aug 21, 2014 at 12:53
  • Hope this link:[stackoverflow.com/a/483165/1982677] helps. Commented Aug 23, 2014 at 9:36

1 Answer 1

3

It is essential to set "python.path" for the PythonInterpreter so that it can load your the as1 module. To do that, you have to initialize PythonInterpreter in this way:

Properties properties = System.getProperties();
properties.put("python.path", PATH_TO_PARENT_DIRECTORY_OF_AS1_PY);
PythonInterpreter.initialize(System.getProperties(), properties, new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("calen.py");
answered Feb 23, 2015 at 18:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.