import another python script, using jython

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

import another python script, using jython

pz3977
I am trying to execute a python method from eclipse using jython. I managed to run it with following code:

PythonInterpreter.initialize(System.getProperties(), 
                         System.getProperties(), new String[0]); 
     
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("Mypython.py"); 
interpreter.eval("MyClassName().MyMethodName()")

My problem is when I import another python script which exists even in the same directory with Mypython.py. For example, when I add:

from food import Pizza

to Mypython.py, it starts to complain that cannot import. ImportError..

I found some questions about importing python libaries like os, but in my case this is not an issue.

I tried to make the folder as a package, add __init__.py etc but it failed.  I saw  some people use PySystemState, but I think it is for jython modules not user python scripts. if this is the solution please give me a simple example.

Could you please help me with that problem.


------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: import another python script, using jython

Jeff Emanuel
PySystemState is the sys module.  You need to initialize sys.path.  You can do it
by invoking python code in the interpreter, or by calling methods of PySystemState
from Java.  I do it like this:

      String initScript =
        "import sys\n" +
        "sys.path.insert(0,'"+home+"')\n" +
        "sys.path.append('"+jarFilePath+"')\n" +
        "sys.path.append('"+jarFilePath+"/Lib')\n";

      interp.exec(initScript);


On 8/1/2014 12:39 PM, Parisa Zahedi wrote:
I am trying to execute a python method from eclipse using jython. I managed to run it with following code:

PythonInterpreter.initialize(System.getProperties(), 
                         System.getProperties(), new String[0]); 
     
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("Mypython.py"); 
interpreter.eval("MyClassName().MyMethodName()")

My problem is when I import another python script which exists even in the same directory with Mypython.py. For example, when I add:

from food import Pizza

to Mypython.py, it starts to complain that cannot import. ImportError..

I found some questions about importing python libaries like os, but in my case this is not an issue.

I tried to make the folder as a package, add __init__.py etc but it failed.  I saw  some people use PySystemState, but I think it is for jython modules not user python scripts. if this is the solution please give me a simple example.

Could you please help me with that problem.



------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds


_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users


------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: import another python script, using jython

pz3977
Dear Jeff,

Thanks for your reply.
I assigned the location of my python files to "home" and jar file path to "jarFilePath". When I tried it, the error changed:

It says IOError: (2, 'File not found' --.../imageFileIo.py (Permission denied)) while I have no settings on the folder. Is it because java does not have access to folder? how can I solve it?

Regards,
Parisa




On Fri, Aug 1, 2014 at 8:57 PM, Jeff Emanuel <[hidden email]> wrote:
PySystemState is the sys module.  You need to initialize sys.path.  You can do it
by invoking python code in the interpreter, or by calling methods of PySystemState
from Java.  I do it like this:

      String initScript =
        "import sys\n" +
        "sys.path.insert(0,'"+home+"')\n" +
        "sys.path.append('"+jarFilePath+"')\n" +
        "sys.path.append('"+jarFilePath+"/Lib')\n";

      interp.exec(initScript);



On 8/1/2014 12:39 PM, Parisa Zahedi wrote:
I am trying to execute a python method from eclipse using jython. I managed to run it with following code:

PythonInterpreter.initialize(System.getProperties(), 
                         System.getProperties(), new String[0]); 
     
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("Mypython.py"); 
interpreter.eval("MyClassName().MyMethodName()")

My problem is when I import another python script which exists even in the same directory with Mypython.py. For example, when I add:

from food import Pizza

to Mypython.py, it starts to complain that cannot import. ImportError..

I found some questions about importing python libaries like os, but in my case this is not an issue.

I tried to make the folder as a package, add __init__.py etc but it failed.  I saw  some people use PySystemState, but I think it is for jython modules not user python scripts. if this is the solution please give me a simple example.

Could you please help me with that problem.



------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds


_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users



------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls.
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: import another python script, using jython

Jeff Emanuel

If the file system permissions are set correctly, perhaps you have another program using your file and preventing other access to it. 


On 8/4/2014 2:48 AM, Parisa Zahedi wrote:
Dear Jeff,

Thanks for your reply.
I assigned the location of my python files to "home" and jar file path to "jarFilePath". When I tried it, the error changed:

It says IOError: (2, 'File not found' --.../imageFileIo.py (Permission denied)) while I have no settings on the folder. Is it because java does not have access to folder? how can I solve it?

Regards,
Parisa




On Fri, Aug 1, 2014 at 8:57 PM, Jeff Emanuel <[hidden email]> wrote:
PySystemState is the sys module.  You need to initialize sys.path.  You can do it
by invoking python code in the interpreter, or by calling methods of PySystemState
from Java.  I do it like this:

      String initScript =
        "import sys\n" +
        "sys.path.insert(0,'"+home+"')\n" +
        "sys.path.append('"+jarFilePath+"')\n" +
        "sys.path.append('"+jarFilePath+"/Lib')\n";

      interp.exec(initScript);



On 8/1/2014 12:39 PM, Parisa Zahedi wrote:
I am trying to execute a python method from eclipse using jython. I managed to run it with following code:

PythonInterpreter.initialize(System.getProperties(), 
                         System.getProperties(), new String[0]); 
     
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("Mypython.py"); 
interpreter.eval("MyClassName().MyMethodName()")

My problem is when I import another python script which exists even in the same directory with Mypython.py. For example, when I add:

from food import Pizza

to Mypython.py, it starts to complain that cannot import. ImportError..

I found some questions about importing python libaries like os, but in my case this is not an issue.

I tried to make the folder as a package, add __init__.py etc but it failed.  I saw  some people use PySystemState, but I think it is for jython modules not user python scripts. if this is the solution please give me a simple example.

Could you please help me with that problem.



------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds


_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users




------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls.
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users