Import rethinkdb python module in to use in Jython - python

I am trying to connect to rethink db(has a python driver) from my java app.
RethinkDB python install section https://www.rethinkdb.com/docs/install-drivers/python/ does not have a place to explicitly download the python modules. Because for me, I am trying to use jython to connect and it fails with following
code:
String s = "import rethinkdb as r\n" +
"r.connect('localhost', 28015).repl()\n" +
"r.table('tv_shows').insert({ 'name': 'Star Trek TNG' }).run()";
PyCode code = python.compile(s);
python.exec(code);
System.out.println("Done..");
Error:
Exception in thread "main" Traceback (most recent call last):
File "", line 1, in
ImportError: No module named rethinkdb
Any idea on where can I get the rethinkdb module explicitly and how I can made it available to my Java app ? (assume to out that into class path)

The currently released Python driver is always up on PyPi, and you can download it directly from: https://pypi.python.org/pypi/rethinkdb
I am not a Jython user, so I can best direct you to this question about modules: How can I install various Python libraries in Jython?
I should also note that an official Java driver was recently released: http://rethinkdb.com/blog/official-java-driver/

Related

Running SikuliX 1.1.4 Python scripts from Command Line

I have some Python (jython actually) scripts that run with Sikulix.
I was previously using version 1.1.1 and was using the command line (after doinmg the setup):
java -Dsikuli.Debug=-2 -cp sikulix.jar org.python.util.jython main.py
With version 1.1.4, there is no more setup, and jython has been removed from sikulix.jar and sikulixapi.jar. Jython is in another jar file (jython-standalone-2.7.1.jar).
I tried to run with following command line
java -Dsikuli.Debug=-2 -cp "sikulix.jar;jython-standalone-2.7.1.jar" org.python.util.jython main.py
But I get the following error
Traceback (most recent call last):
File "test.py", line 3, in <module>
from sikuli.Sikuli import *
ImportError: No module named sikuli
The documentation is not fully updated on how to do it. They mention installation of jython, jip and other stuff but nothing managed to work.
Any idea on how to do it?
Thanks
==PS==:
After doing the following it almost worked:
Installing jython
Setting CLASSPATH to absolute path of sikulixapi.jar
Running jython main.py
I got the following error:
[error] RunTimeINIT: *** terminating: Java arch not 64 Bit or not detected (java 8-32 version 1.8 vm 25.121-b13 class 52.0 arch null)
I have installed Jython with a 32-bits Java and it seems 64-bits Java is required.
I will probably try again with 64-bits Java JDK.
Add the following line at the beginning of your script
import org.sikuli.script.SikulixForJython
This will help to look for sikuli module in Java classes.
You can then run with the command line mentionned previously:
java -cp "sikulixapi.jar;jython-standalone-2.7.1.jar" org.python.util.jython main.py
Example of Python script (main.py):
import org.sikuli.script.SikulixForJython
from sikuli.Sikuli import *
notepad = App('notepad.exe')
notepad.open()
sleep(1)
type("It is working!")
notepad.close()

Having problems using the python code generated from the google protocol buffer

I am getting the following error when I try to run gtfs_realtime_pb2.py (the python code generated from running the gtfs-realtime.proto through google's protocol buffer):
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
from google.protobuf import descriptor
ImportError: No module named 'google'
This is a link to the specific code I am having trouble with:
https://github.com/mattwigway/gtfsrdb
And the link to installing google's protocol buffer:
https://developers.google.com/protocol-buffers/
I am pretty certain I have install the buffer correctly, so why is it throwing up that it can't find this module?
Included in protocol buffer is descriptor.proto; python code for this proto should of been generated as part of the install process.
The protocol buffer you are using uses descriptor.proto so the directory where it resides needs to be included via the --proto_path= parameter (see Importing "google/protobuf/descriptor.proto" in java protocol buffers
For window systems, the install directory will be where you placed it; for *nix it will probably be answer taken from
/usr/include/descriptor.proto
or
/usr/local/include/descriptor.proto
i.e. for java you would do something like
protoc addressbookSD.proto --java_out=./ --proto_path=./ --proto_path=<protobuf-install-directory>/src
where <protobuf-install-directory> is the protocol buffers install directory. The key point is descriptor.proto is in
<protobuf-install-directory>\src\google\protobuf

python select.epoll() not working

I build python2.6.1 on laptop. Build was successful. After that I tried to run python using ./python and tried to import select module
>>>import select
>>>select.epoll()
<select.epoll object at 0xb76140d0>
After that I copied the python build folder to anther laptop and tried to run python
>>>import select
>>> select.epoll()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'epoll'
How to solve this problem
From the manual:
select.epoll([sizehint=-1])
(Only supported on Linux 2.5.44 and newer.)
Returns an edge polling object, which can be used as Edge or Level Triggered interface
for I/O events; see section Edge and Level Trigger Polling (epoll)
Objects below for the methods supported by epolling objects.
New in version 2.6.
And obviously distromakers can disable it in python build settings or so. It does depend on glibc only as far as I know. Also are you sure you are importing from the system select module, and not from your own module named by the same name? (check select.__file__; as a builtin module it should not have a file ;)

How to import 'GDB' in Python

I am using Python 2.7 and Python 3.1.3. But in my Python I am unable to "import gdb".
It is giving me an error as:
>>> import gdb
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ImportError: No module named gdb
>>>
What's a reason for this? How should I solve this problem?
import gdb only works when your Python code is running within the GDB process. It's not supposed to work from the regular system Python interpreter.
Explanation
GDB embeds the Python interpreter so it can use Python as an extension language.
You can't just import gdb from /usr/bin/python like it's an ordinary Python library because GDB isn't structured as a library.
What you can do is source MY-SCRIPT.py from within gdb (equivalent to running gdb -x MY-SCRIPT.py).
Example Program
Here's a self contained example. Save the file below to t.py:
import gdb
gdb.execute('file /bin/cat')
o = gdb.execute('disassemble exit', to_string=True)
print(o)
gdb.execute('quit')
run:
$ gdb -q -x t.py
and you'll see the PLT stub for exit() disassembled. On x86-64 Linux:
Dump of assembler code for function exit#plt:
0x0000000000401ae0 <+0>: jmpq *0x20971a(%rip) # 0x60b200 <exit#got.plt>
0x0000000000401ae6 <+6>: pushq $0x3d
0x0000000000401aeb <+11>: jmpq 0x401700
End of assembler dump.
I've collected some resources on learning the GDB Python API here.
You can follow this tutorial to install PythonGDB. The Python code depends on a C extension.
For Windows, there is a recent enough gdb build in MinGW, but it doesn't seem to include the Python module you can import (still supports Python scripting in gdb). You have to install MinGW and then install the gbd package using mingw-get install gdb.
If you use Cygwin, there's again a recent enough gdb in Cygwin Ports, without a Python module but with Python scripting support.
I suppose it'd be possible to build gdb from source in either platform and get the Python module.
I just ran into the similar situation when trying to debug Webkit:
$ python Tools/gdb/webkit.py
Traceback (most recent call last):
File "Tools/gdb/webkit.py", line 38, in <module>
import gdb
ImportError: No module named gdb
I then realized that this script should be invoked in gdb to make it working:
(gdb) source Tools/gdb/webkit.py
(gdb) p run
$1 = (const WebCore::TextRun &) #0x7fffffffa450: {m_characters = "Plugin Testsa", m_len = 12, m_xpos = 0,
m_padding = 0, m_allowTabs = false, m_rtl = false, m_directionalOverride = false,
m_applyRunRounding = true, m_applyWordRounding = true, m_disableSpacing = false}
Hope this helps.
I can't test now, but I think you need to configure and build a Python-enabled GDB. Take a look at this guide.
I hope that helps.
This is outdated, I think. Anyway, you always need to build and configure a Python enabled GDB.
You can script GDB using the Python programming language. This feature is available only if GDB was configured using --with-python.
You have to configure GDB using that option:
--with-python=location
Where location is the location of python you would like to use GDB with.

Do any one have gdb.py file or its link [duplicate]

I am using Python 2.7 and Python 3.1.3. But in my Python I am unable to "import gdb".
It is giving me an error as:
>>> import gdb
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ImportError: No module named gdb
>>>
What's a reason for this? How should I solve this problem?
import gdb only works when your Python code is running within the GDB process. It's not supposed to work from the regular system Python interpreter.
Explanation
GDB embeds the Python interpreter so it can use Python as an extension language.
You can't just import gdb from /usr/bin/python like it's an ordinary Python library because GDB isn't structured as a library.
What you can do is source MY-SCRIPT.py from within gdb (equivalent to running gdb -x MY-SCRIPT.py).
Example Program
Here's a self contained example. Save the file below to t.py:
import gdb
gdb.execute('file /bin/cat')
o = gdb.execute('disassemble exit', to_string=True)
print(o)
gdb.execute('quit')
run:
$ gdb -q -x t.py
and you'll see the PLT stub for exit() disassembled. On x86-64 Linux:
Dump of assembler code for function exit#plt:
0x0000000000401ae0 <+0>: jmpq *0x20971a(%rip) # 0x60b200 <exit#got.plt>
0x0000000000401ae6 <+6>: pushq $0x3d
0x0000000000401aeb <+11>: jmpq 0x401700
End of assembler dump.
I've collected some resources on learning the GDB Python API here.
You can follow this tutorial to install PythonGDB. The Python code depends on a C extension.
For Windows, there is a recent enough gdb build in MinGW, but it doesn't seem to include the Python module you can import (still supports Python scripting in gdb). You have to install MinGW and then install the gbd package using mingw-get install gdb.
If you use Cygwin, there's again a recent enough gdb in Cygwin Ports, without a Python module but with Python scripting support.
I suppose it'd be possible to build gdb from source in either platform and get the Python module.
I just ran into the similar situation when trying to debug Webkit:
$ python Tools/gdb/webkit.py
Traceback (most recent call last):
File "Tools/gdb/webkit.py", line 38, in <module>
import gdb
ImportError: No module named gdb
I then realized that this script should be invoked in gdb to make it working:
(gdb) source Tools/gdb/webkit.py
(gdb) p run
$1 = (const WebCore::TextRun &) #0x7fffffffa450: {m_characters = "Plugin Testsa", m_len = 12, m_xpos = 0,
m_padding = 0, m_allowTabs = false, m_rtl = false, m_directionalOverride = false,
m_applyRunRounding = true, m_applyWordRounding = true, m_disableSpacing = false}
Hope this helps.
I can't test now, but I think you need to configure and build a Python-enabled GDB. Take a look at this guide.
I hope that helps.
This is outdated, I think. Anyway, you always need to build and configure a Python enabled GDB.
You can script GDB using the Python programming language. This feature is available only if GDB was configured using --with-python.
You have to configure GDB using that option:
--with-python=location
Where location is the location of python you would like to use GDB with.

Categories

Resources