0

I am trying to run a python script from java; in py script I am using pywinauto package and want to initiate notepad++. When I am running this script it works fine, however when same is called from Java main method it throws TypeError: __enter__(): expected 1 args; got 0.

Below is python script:

import sys
sys.path.append("C:\\jython2.7.0\\bin")
sys.path.append("C:\\Python27")
sys.path.append("C:\\Python27\\Lib")
sys.path.append("C:\\jython2.7.0\\Lib")

def runpradeep():
    print "trying to run it"
    from pywinauto.application import Application
    app = Application().start('C:\\Program Files (x86)\\Notepad++\\notepad++.exe')
    app.kill()

runpradeep()

and below is java class:

package himalaya;

import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.Properties;

public class OpenBoApplication {
public static void main(String[] args) throws ScriptException {
    Properties props = new Properties();
    props.put("python.home", "C:\\Python27");
    props.put("python.console.encoding", "UTF-8");
    props.put("python.security.respectJavaAccessibility", "false");
    props.put("python.import.site", "false");
    Properties preprops = System.getProperties();
    PythonInterpreter.initialize(preprops, props, new String[0]);
    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.execfile("D:path to py script\\renatoplease.py");

    interpreter.exec("runpradeep()");
}

}

Below is error log:

Exception in thread "main" Traceback (most recent call last): File "D:\pbhardwa\IdeaProjects\javapythongroovy\src\com\ingenico\runmiddleware\renatoplease.py", line 16, in runpradeep() File "D:\pbhardwa\IdeaProjects\javapythongroovy\src\com\ingenico\runmiddleware\renatoplease.py", line 11, in runpradeep from pywinauto.application import Application File "pyclasspath/pywinauto/application.py", line 72, in File "C:\Python27\Lib\multiprocessing__init__.py", line 65, in from multiprocessing.util import SUBDEBUG, SUBWARNING File "C:\Python27\Lib\multiprocessing\util.py", line 39, in import threading # we want threading to install it's

File "C:\Python27\Lib\threading.py", line 1191, in _shutdown = _MainThread()._exitfunc

File "C:\Python27\Lib\threading.py", line 1083, in init self._Thread__started.set()

File "C:\Python27\Lib\threading.py", line 582, in set """

File "C:\Python27\Lib\threading.py", line 286, in enter return self.lock.__enter()

TypeError: enter(): expected 1 args; got 0

I am using Intellij ultimate edition and Pycharm plugin is installed. I have also installed pywinauto(as mentioned enter link description here), pythonpath is also set. Also added jython dependencies to class path. Attaching complete project

Any help will surely help us.

PradeepB
  • 11
  • 1
  • Jython is a different interpreter than pure Python 2.7. Not sure it’s possible to use package installed in another Python. I think you have to install pywinauto on Jython (though I never tried it). – Vasily Ryabov Apr 03 '18 at 10:23
  • Hi Vasily, thanks for the reply.. I am trying to get Jython on Intellij but getting this error - `Traceback (most recent call last): File "C:\jython2.7.0\Lib\shutil.py", line 208, in copytree raise Error, errors shutil.Error: [('C:\\jython2.7.0\\Lib\\encodings', 'D:\\pbhardwa\\IdeaProjects\\javapythongroovy\\venv2\\Lib\\encodings', "[Errno 1] Operation not permitted: 'D:\\\\pbhardwa\\\\IdeaProjects\\\\javapythongroovy\\\\venv2\\\\Lib\\\\encodings'")]` – PradeepB Apr 03 '18 at 15:53
  • hi, I moved onto jython 2.7.1 and stuck with same mentioned in actual issue. I think pywinauto do not work with Jython and now I am thinking to move to Sikuli which actually I don't want to, but have no other option. However a suggestion if pywinauto could work with jython than for sure this can be integrated with cucumber, serenity etc . – PradeepB Apr 04 '18 at 13:44
  • I've not tried pywinauto on Jython yet. But maybe these answers could help: https://stackoverflow.com/questions/8898765/calling-python-in-java – Vasily Ryabov Apr 04 '18 at 19:05
  • Someone already asked about Jython: https://stackoverflow.com/q/30869828/3648361 It seems pyWin32 extensions can't be installed on Jython. Technically it's possible to avoid pyWin32 usage, but it requires plenty of work. – Vasily Ryabov Apr 04 '18 at 19:16
  • Are you considering Cucumber based solution only? Maybe Robot Framework is a good alternative written in Python. – Vasily Ryabov Apr 05 '18 at 21:32

0 Answers0