Running SikuliX 1.1.4 Python scripts from Command Line - python

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()

Related

Executing a python script from scala giving error while running the same script from the command line does NOT

This is my python script:
!/usr/bin/python
import sys
import magic
m=magic.from_file('<file with absolute path goes here>')
print(m)
On running this from the command line:
$ python script.py
Microsoft Word 2007+
results in the output of the TYPE of the document that file is. Using python-magic.
Now running the same script from scala using below code:
import sys.process._
def compWithLibmagic(){
val result = "python /script.py" !
}
throws the below error:
Traceback (most recent call last):
File "script.py", line 3, in <module>
import magic
ImportError: No module named magic
PS: I have both python 2.7 and python 3.6 installed on my machine and running the script using any of them from the command line runs just fine so I guess both of them are bundled with the MAGIC packages correctly.
Would highly appreciate any kind of help.
I will try to answer my own question in the best possible way.
While trying to execute the script.py from the command line using python compiler python2.7 was being used and when I tried to invoke the same script.py from the scala compiler using
import sys.process._
def compWithLibmagic(){
val result = "python /script.py" !
}
it was using python3 which was getting unnoticed. It took me a couple of days to actually figure this thing out and finally I removed all the python2.7 and python3 libraries and installed python 2.7 again and it worked like a charm.
It works from both python3 command and Scala code for me.
This should work.
import sys.process._
def compWithLibmagic(){
val result = "python3 script.py".!!
}

MySQLdb module works when running Python script from the command line, but not as a cron job

I am trying to create a cron job to run a python script I wrote, but for some reason the script only runs when I'm running it from the command line. It worked before, but I accidentally deleted my crontab. When the crontab I just wrote tries to run it, I get an error about the MySQLdb module being the wrong architecture:
Traceback (most recent call last):
File "/Users/myuser/Documents/EE_SFDC Integration/csv_reader/Weekly_Updates/Weekly_Confirmation/weekly_confirm.py", line 7, in <module>
import MySQLdb
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): no suitable image found. Did find:
/Library/Python/2.7/site-packages/_mysql.so: mach-o, but wrong architecture
In my crontab, I've tried using the full path to several python versions (2.7 and 3.4), but I still get the issue. When I use 3.4, I don't get an error about MySQLdb, but I do get an error on a print .forma() statement being the wrong syntax.
But again, that doesn't happen when I run the script from the command line, so I expect this is not the correct version either.
The cron job looks like this:
14 12 * * * today=`date +"\%Y-\%m-\%d"` && /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2 /Users/myuser/Documents/EE_SFDC\ Integration/csv_reader/Weekly_Updates/Weekly_Confirmation/weekly_confirm.py > /Users/myuser/Documents/EE/run_logs/weekly_sync_runlog_$today.txt 2>&1
Any help is much appreciated!
The error message typically means that you have a mismatch between the MySQLdb (or a dependency) and Python architecture.
That is, one of them are most likely 64-bit, while the other is 32-bit.
What happens if you try the following in a terminal?
$ /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2
(Take note of the exact version)
>>> import platform
>>> print platform.architecture()
>>> import MySQLdb
Do it again, but this time with python from your path and see if they differ
$ python
>>> import platform
>>> print platform.architecture()
>>> import MySQLdb
Edit:
You might also check with file, what architecture the module is built for.
$ file /Library/Python/2.7/site-packages/_mysql.so
/Library/Python/2.7/site-packages/_mysql.so: Mach-O universal binary with 2 architectures
/Library/Python/2.7/site-packages/_mysql.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
/Library/Python/2.7/site-packages/_mysql.so (for architecture i386): Mach-O bundle i386

error in running Google apps in python in windows OS

in google app engine
get error in runing the appicaition
in logs of googleappengine
2012-04-21 13:24:04 Running command: "['C:\Python32\pythonw.exe', 'C:\Program Files\Google\google_appengine\dev_appserver.py', '--admin_console_server=', '--port=8080', 'C:\udacity\googleapps\hello-udacity']"
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\dev_appserver.py", line 125, in
run_file(file, globals())
File "C:\Program Files\Google\google_appengine\dev_appserver.py", line 121, in run_file
execfile(script_path, globals_)
NameError: global name 'execfile' is not defined
2012-04-21 13:24:07 (Process exited with code 1)
You appear to be running Python 3.2 (your Python executable is on this path 'C:\Python32\pythonw.exe').
Google AppEngine requires Python 2.5 or 2.7 at this stage, see this article. You need to download Python 2.7, install it in a parallel directory, and make it the default executable.
You may find it helpful to read this answer on running more than one version of Python
Also, I had the same problem. This is because I have installed both Python 2.7 and Python 3.x (the latest version of the framework Django uses the latter). I installed Python 3.x later so this is the version that is used by default in my system. To use Python 2.7 you could do the following:
C:\Python27\python.exe dev_appserver.py path-to-your-app
Notice that here
C:\Python27\python.exe
is the path to the python 2.7 executable (in my case, but this could vary).
I did that and it worked.
More info and reading here.

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