Calling Java jar class with parameter from Python
Hi, I have read a number of posts on how this can be done, but I have not been able to replicate success with the particular command I'm wishing to execute. I am wanting to execute the following Java command from Python in Windows: java -cp c:\antlr\antlr-3.4-complete.jar org.antlr.Tool "C:\Users\Jason\Documents\antlr\java grammar\Java.g" This command works outside of Python at the command prompt. So I do the same using Python's os.system: os.system("C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe -cp c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool 'C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g'") The return value is 1, which presumably indicates error. Another variation using subprocess.call, one of many tried, gives the same result: subprocess.call(["C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", "-cp c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool", "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"] ) This variation using subprocess.Popen, gives a result, but it's ('', None). subprocess.Popen(["C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", "-cp c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool", "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"], stdout=subprocess.PIPE, shell=True ).communicate() Obviously, some trick is being missed. Could anyone shed light on what it may be? Thanks Jason -- http://mail.python.org/mailman/listinfo/python-list
Basic python extension producing error: "dynamic module not initialized properly"
Hi, I am trying to create an extension to call into a c++ library, a library which simply returns a string, given a string. At first, a quick and simple approach was tried (based on post http://stackoverflow.com/questions/1615813/how-to-use-c-classes-with-ctypes) in which the usual infrastructure for loading extensions is bypassed. A working simple example is shown beneath: myextension.cpp #include extern "C" { int test_func() { return 10; } } To execute in Python: from ctypes import cdll mlib=cdll.LoadLibrary("myextension.dll") print( mlib.test_func() ) This works fine when function return type is a number, but not when a string. C++ code returns char *, which is received by Python as a pointer, which prints as a number, eg1755066468. Apparently, based on web posts viewed, it seems data conversion is required, using a function such as Py_BuildValue("s", str) from python.h. But this gave a similar result, in which a pointer is received by Python instead of a string. The modified function used in this case: PyObject * test_func() { return Py_BuildValue("s", "aString"); } Resort was then made to the conventional approach of defining a method table and initializing method. (Abandoned cdll.LoadLibrary(...) as subsequently test_func was not recognised, and instead put the dll (.dll changed to pyd) in /python26/DLLs.) The revised code: myextension.cpp #include extern "C" { static PyObject * test_func(PyObject *self, PyObject *args) { return (PyObject *) 0; } static PyMethodDef TestMethods[] = { {"test", test_func, METH_VARARGS, "test"}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC initlibTestExtnModule(void) { (void) Py_InitModule("TestExtnModule", TestMethods); } } Using the import statement "import libTestExtnModule" however produced the following error message: "SystemError: dynamic module not initialized properly" As far as I know there is nothing wrong with the initialisation of the extension code above. Perhaps the error is related to compilation. I have not used distutils, but rather compiled externally and dropped the dll (as pyd) into the /DLLs directory as mentioned. This should work, as far as I know. Related to compilation, both MinGW and Python2.6 are 32 bit versions, and g++ statements used were: mingw32-g++ "-IC:\\Python26\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o myextension.o "..\\myextension.cpp" mingw32-g++ -LC:/Python26/libs -shared -o libTestExtnModule.pyd myextension.o -lpython26 Any suggestions as to why the dynamic module is not initialising properly, or as to how otherwise a string can be returned, would be greatly appreciated. Thanks Jason -- http://mail.python.org/mailman/listinfo/python-list
Error importing __init__ declared variable from another package
Hi, I have a simple configuration of modules as beneath, but an import error is reported: /engine (__init__ is empty here) engine.py /sim __init__.py The module engine.py imports a variable instantiated in sim.__init__ as follows: from sim import var_name var_name.func() The following error messaged is received on the func() call above (Eclipse PyDev): "undefined variable from import: func" Any idea why this is causing an error? -- http://mail.python.org/mailman/listinfo/python-list
Re: Error importing __init__ declared variable from another package
Accidentally hit post by mistake before msg completed. Any comments appreciated. It's a very simple scenario, but not sure what the mistake is. Thanks Jason On Tue, Feb 28, 2012 at 6:55 PM, Jason Veldicott wrote: > Hi, > > I have a simple configuration of modules as beneath, but an import error > is reported: > > /engine >(__init__ is empty here) >engine.py > /sim >__init__.py > > > The module engine.py imports a variable instantiated in sim.__init__ as > follows: > >from sim import var_name >var_name.func() > > The following error messaged is received on the func() call above (Eclipse > PyDev): > > "undefined variable from import: func" > > Any idea why this is causing an error? > -- http://mail.python.org/mailman/listinfo/python-list
Re: Error importing __init__ declared variable from another package
> > > Hi, > > > > I have a simple configuration of modules as beneath, but an import error > > is reported: > > > > /engine > >(__init__ is empty here) > >engine.py > > /sim > >__init__.py > > > > > > The module engine.py imports a variable instantiated in sim.__init__ as > > follows: > > > >from sim import var_name > >var_name.func() > > > > The following error messaged is received on the func() call above > (Eclipse > > PyDev): > > > > "undefined variable from import: func" > Are you rephrasing or is this really the error message? If so run your > program again on the command-line. Then please cut and paste the error > message together with the traceback. > > Any idea why this is causing an error? > What version of Python are you using? > What does sim/__init__.py contain? Thanks Peter. I'm using Python 2.6, but it works at the command line. The error only appears in Eclipse as a red cross in the margin. The exact error msg, as appears in a floating text caption on mouse over, is as I mentioned (capitalised). Perhaps it is some issue in PyDev, maybe related to the version of Python I'm using. I'm in the process of trying to solve another related import problem, and wished to resolve this one in the hope that it might shed light on the other. But as it works beside the error icon appearing, I might just ignore it and spare the trouble of precise identification of cause. Jason -- http://mail.python.org/mailman/listinfo/python-list
Debug python from DLL callback?
Hi, I am wondering if anyone here might be able to suggest if there is a way of switching over from python execution into debug mode of an IDE, from python code that is executed as a callback from a C++ DLL? Thanks Jason -- http://mail.python.org/mailman/listinfo/python-list
How to remove ellipses from long paths in traceback?
Hi, Long paths in python traceback are contracted with ellipses. eg: TclError: couldn't load library "C:/Python26/tcl/tk8.5/../../bin/tk85.dll" Is there any way to see the full path? Surprisingly, search didn't reveal an answer to this question. Thanks Jason -- http://mail.python.org/mailman/listinfo/python-list