How do I read from stdin with monkeyrunner? I tried the following code.
import sys
print("type something")
something = sys.stdin.readline()
print(something)
I also tried the following:
print("type something")
something = raw_input()
print(something)
In both cases the program prints "type something" but it does not respond after I type something. It seems i am making some silly mistake?
This seems to be a bug with Jython 2.5.3, the version that is included with MonkeyRunner. The issue says Mac OS X but I am able to reproduce on Ubuntu.
To fix it, you can download the Jython 2.5.4rc1 standalone Jar from the Jython website (download link) and copy it into the $SDK/tools/lib directory. Note that the Jython 2.7.0 standalone Jar will not work properly. You don't need to rename the new Jar, but you do need to (re)move the old jython-standalone-2.5.3.jar from the directory.
Another option (if you only need user input) is to use the input function in the MonkeyRunner class.
Related
I am new to Jython and Python, trying to build a prototype that makes use of Python code to be called from within Java. The code I am developing works in Jetty and in standalone mode (running java -jar from the command line), but not when deployed to weblogic.
How can I make weblogic(10.3.5) server/Jython recognize the Lib folder within jython-standalone-2.5.4-rc1.jar?
My Java code uses the JythonObjectFactory to invoke python modules as outlined in the Jython book:
http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html
The Python modules are using external libraries like csv, logging etc. that are not packaged with jython.jar, hence I am using jython-standalone jar.
The java code includes an interface that would define the class type of the first invoked py module from within java. The interface and the input and output (to python modules) type classes are in a package structure as com.abc.xpackage. and the py modules exist at the root of this package. A controller layer calls the objectfactory and in turn executes the python code thus:
JythonObjectFactory calFactory = new JythonObjectFactory(CalcType.class, "Calculate", "Calculate");
CalcType engine = (CalcType)calFactory.createObject();
output = engine.execute(input);
The entire code is bundled as a jar file which would become part of a web application deployed on weblogic. The code was compiled with maven (with jython dependencies included in the repository) and runs fine on the included Jetty runtime within eclipse.
When deployed on weblogic, however, I get a "ImportError: no module named csv" error.
To analyze what is happening, I tried printing the Jython system state path on weblogic and the standalone environment/Jetty. What I found is,
on Jetty, the system path consists of the following:
C:\.m2\repo\org\python\jython\jython-standalone-2.5.3-rc1.jar\Lib, ____classpath__, ____pyclasspath__
on Weblogic, printing the system path by default shows the following:
____classpath__, ____pyclasspath__
I tried forcing the inclusion of the missing path using the code as follows:
public JythonObjectFactory(PySystemState state, Class interfaceType, String moduleName, String className) {
String pathToAppend = new File(state.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath()+"\\Lib";
state.path.insert(0, new PyString(pathToAppend));
state.path.append(new PyString(pathToAppend));
System.out.println("Jython sys path: "+state.path);
Please note, I prepended as well as appended the path in different trials. The sys path on weblogic now displays the following:
Jython sys path: ['C:\\wldomain\\wls135\\servers\\cgServer\\tmp\\app-1\\war\\WEB-INF\\lib\\jython-standalone-2.5.4-rc1.jar\\Lib', '__classpath__', '__pyclasspath__/', 'C:\\wldomain\\wls135\\servers\\cgServer\\tmp\\app-1\\war\\WEB-INF\\lib\\jython-standalone-2.5.4-rc1.jar\\Lib']
I am still getting ImportError despite this forcing of sys path. Please help why this works in a local environment, and not on weblogic, and if there is any configuration I am missing. Apologize for the rambling long post, I did not know how to explain the problem better. I will try and include any code/artifacts as needed.
Based on a comment(by Lassi) on the blog post below:
http://www.petervannes.nl/files/e1c3c56d15d25dcfd4adb5397a9ef71e-53.php
The jython issue was resolved after explicitly adding the Lib folder python.path to the weblogic startup script as a JAVA_OPTION.
In my case I added the exploded Lib folder to the domain server lib, but based on my test this works also from within the jython jar. Both the following JAVA_OPTIONS worked:
-Dpython.path=C:\wldomain\wls135\lib\Lib
-Dpython.path=C:\wldomain\wls135\lib\jython-standalone-2.5.4-rc1.jar\Lib
The programmatic way of sys.path.append worked for the local environment(jetty) but did not seem to work for weblogic.
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.
sys.path is your module-import search path. You can import sys and then modify sys.path as required.
I'm trying out casperJS to run some automated tests. On windows. I followed this installation guide but am stuck.
when I try to run a simple test from the command line like this:
casperjs.bat smoketest.js
I get the following error:
Error: Cannot find module 'C:/code/base/main/Shared/casperjs\modules\cli.js'
I checked, and the file cli.js does exist under the specified path.
I thought the problem could be the mixture of forward and backward slashes in the path, but I edited the bootstrap.js casper code to hard code the path and it made no difference - it still the cannot find module.
I'm have tried this using phantomJS version 1.9.0, and 1.8.2.
Anyone know what I am doing wrong?
Has anyone had success running casper on Windows?
This is due to a bug in PhantomJS 1.9.0 that does not allow for absolute path loading in Windows. Afaik, this should only be apparent if you are using a master build of CasperJS (v1.1).
This bug has been fixed, but is not yet in the Windows binary. You can compile it yourself if you'd like, but that can take quite a bit of time.
I was using the master build of casper. With the latest released version, 1.0.2, it works fine.
Every time when I try to start my mapreduce application (in standalone Hadoop), it tries to put stuff in the tmp directory, which it can't:
Exception in thread "main" java.io.IOException: Failed to set permissions of path: \tmp\hadoop-username\mapred\staging\username-1524148556\.staging to 0700
It ties to use an invalid path (slashes should be the other way around for cygwin).
I set hadoop.tmp.dir in core-site.xml (in the conf folder of Hadoop), but it seems that the config file is never read (if I put syntax errors in the file, it makes no difference). I added:
--config /home/username/hadoop-1.0.1/conf
To the command, but no difference. I also tried:
export HADOOP_CONF_DIR=/home/username/hadoop-1.0.1/conf
but also that does not seem to have an effect....
Any pointers on why the configs would not be read, or what else I am failing to see here?
Thanks!
It's not that the slashes are inverted, it's that /tmp is a cygwin path which actually maps to /cygwin/tmp or c:\cygwin\tmp. since hadoop is java and doesn't know about cygwin mappings, it takes /tmp to mean c:\tmp.
there's an awful lot of stuff to patch if you want to get 1.0.1 running on cygwin.
see: http://en.wikisource.org/wiki/User:Fkorning/Code/Hadoop-on-Cygwin
I found the following link useful, it seems that the problem stands with newer version of Hadoop. I'm using version 1.0.4 and I'm still facing this problem.
http://comments.gmane.org/gmane.comp.jakarta.lucene.hadoop.user/25837
UPDATED: in Mahout 0.7 and for the ones who use the "Mahoot in Action" book example, you shoud change the example code as follows:
File outFile = new File("output");
if (!outFile.exists()) {
outFile.mkdir();
}
Path output = new Path("output");
HadoopUtil.delete(conf, output);
KMeansDriver.run(conf, new Path("testdata/points"), new Path("testdata/clusters"),
output, new EuclideanDistanceMeasure(), 0.001, 10,
true, 0.1, true);
I am trying to get python-gnupg to work in Jython under PyDev under Eclipse. There is a problem which seems to be caused by the sys.stdin.encoding, which is tested by python-gnupg at it's initialisation.
I found that the following script
import sys
print sys.stdin.encoding
outptus cp850 when I run it from Python standalone in a console and from Jython standalone in a console. When I run it in Python under PyDev, it outputs Cp1252, and when I run it in Jython under PyDev, I get None.
Looking at python-gnupg, both encodings cp850 and Cp1252 seem to be ok, but if it's None, it raises an exception:
File "C:\Python27\lib\site-packages\gnupg.py", line 487, in __init__
self._collect_output(p, result, stdin=p.stdin)
File "C:\Python27\lib\site-packages\gnupg.py", line 561, in _collect_output
stderr = codecs.getreader(self.encoding)(process.stderr)
File "C:\jython2.5.2\Lib\codecs.py", line 920, in getreader
return lookup(encoding).streamreader
at org.python.core.codecs.normalizestring(codecs.java:101)
at org.python.core.codecs.lookup(codecs.java:75)
at org.python.modules._codecs.lookup(_codecs.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException
Looks like there are differences between PyDev and standalone encodings, as well as no (?) encoding for PyDev/Jython? I'd really like to use the convenient PyDev development environment, but how can I get the sys.stdin.encoding to be something sensible when developing a Jython program?
Update 1
Maybe the problem is related to one of the following Jython bug reports:
http://bugs.jython.org/issue1839
http://bugs.jython.org/issue1807 (see msg6717)
But then again, these seem to be independent from PyDev/Eclipse..?
It's actually a Jython issue:
In PySystemState.java, when setting up the encodings, Jython will skip setting any encoding if it's not a tty console:
private void initEncoding() {
String encoding = registry.getProperty(PYTHON_CONSOLE_ENCODING);
if (encoding == null) {
return;
}
for (PyFile stdStream : new PyFile[] {(PyFile)this.stdin, (PyFile)this.stdout,
(PyFile)this.stderr}) {
if (stdStream.isatty()) {
stdStream.encoding = encoding;
}
}
}
So, a workaround could be creating a java module to set that (as encoding is a public field of PyFile, but does not have a setter from the Jython side -- or you could use java reflection to set it)...
Maybe you could ask the Jython guys why this is made in the first place (I think the encoding could be set even if it was not a tty device, but I'm not sure which implications could it have and why it's done that way).
EDIT to summarise the results of the many comments below, between the developers of Jython and PyDev as well as the author of the original question (Philip Jenvey, Fabio Zadrozny and Christian Gelinek, resp.):
During the progress, a couple of test scripts were discussed and developed:
A Python unit test written by Philip to test different encodings set by minimal custom created sitecusomize.py scripts. Fabio thinks that the reason for it not actually setting the encoding is because sys.stdin.encoding is inherited from the parent process instead configured by the unit test script.
A Java test program written by Christian which sets up custom I/O streams for creating a Python subprocess, which was found by Fabio to be more similar to how PyDev sets up the subprocess.
As this didn't work as expected by Fabio, he remembered that PyDev also uses the PYTHONIOENCODING environment variable.
As was pointed out by Philip, the current (2.5) version of Jython doesn't support the PYTHONIOENCODING environment variable, which is used by PyDev (in combination with sitecusomize.py) to set the encoding:
You can log a bug for it, although PYTHONIOENCODING was only added in CPython 2.6, so Jython probably wouldn't support it until 2.7 (Jython's skipping 2.6).
I believe you should be able to explicitly encoding on the launch properties.
Run configurations -> Python run -> Xxx -> Common tab -> Encoding.
PyDev inherits some settings from global Eclipse settings. On Linux the encoding is sane UTF-8 everywhere, but some cranky operating systems cause their own issues..
Also you can probably detect encoding in your .py main module and have PyDev specific hack to fix problems related to your configuration.