1

I am trying to import python paramiko module from java program. So for that i used jython. When i try to import paramiko from jython it gives below error,

Exception in thread "main" Traceback (most recent call last): File "", line 1, in ImportError: No module named paramiko

Please advice me to import paramiko from jython.

public class jythonTest { public static void main(String[] args) throws PyException {

    PythonInterpreter interp = new PythonInterpreter();

    interp.exec("import sys");
    interp.exec("import paramiko");
    interp.exec("import time");
   }

}

silentDev
  • 41
  • 6

1 Answers1

0

This might be because Jython doesn't read the Python packages from the place where you might have installed them in Python through CLI.

One way to solve your problem is to install Paramiko in during the execution of the code:

PythonInterpreter interp = new PythonInterpreter();

interp.exec("from pip._internal import main as pip_main");
interp.exec("pip_main(['install', 'paramiko'])")
interp.exec("import paramiko");

Or

PythonInterpreter interp = new PythonInterpreter();

interp.exec("from pip import main as pip_main");
interp.exec("pip_main(['install', 'paramiko'])")
interp.exec("import paramiko");

Refer to Installing python module within code for more ways to install packages in code depending on your python version. The above should hold good for Python 2.7, which is what I believe Jython is based on.

Speeeddy
  • 154
  • 8