473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

embedded python example: PyString_FromString doesnt work?

I'm trying the embedded python example here:
http://www.python.org/doc/2.3.2/ext/pure-embedding.html

int
main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;

if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_Initialize();
pName = PyString_FromString(argv[1]);

.....
}

But it seems to break at the line;
pName = PyString_FromString(argv[1]);

it always seems to return null whatever string is used as a parameter?
Can anyone explain or provide a working example?

TIA

Jul 18 '05 #1
5 9725
Hi David !

I cannot see anything wrong on your code. So, I'm posting my working
example.

Hint: try to determine, why it is returning NULL (the PyErr_Print()
call)

BranoZ
#include <Python.h>

int
main(int argc, char *argv[]) {
PyObject *s;
int ret;

if (argc < 2)
return -1;

Py_Initialize();

s = PyString_FromString(argv[1]);
if (s == NULL) {
PyErr_Print();
return -1;
}
ret = PyObject_Print(s, stdout, 0);
Py_XDECREF(s);
if (ret < 0) {
PyErr_Print();
return -1;
}

return 0;
}

$ cc test_String.c -o test_String -I /usr/include/python2.3 -lpython2.3
$ ./test_String
$ ./test_String hello
'hello'

Jul 18 '05 #2
On Thu, 24 Mar 2005 06:39:47 -0800, Brano Zarnovican wrote:
Hi David !

I cannot see anything wrong on your code. So, I'm posting my working
example.

Hint: try to determine, why it is returning NULL (the PyErr_Print()
call)

BranoZ


OK your example works fine. I inserted it in the original code:
int
main(int argc, char *argv[])
{
PyObject *pName, *s, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;

if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_Initialize();
pName = PyString_FromString(argv[1]);
pModule = PyImport_Import(pName);

if (pModule == NULL) {
printf( "import went bang...\n");
PyErr_Print();
return -1;
}
....
}

Te test script (again cut and pasted from the python site example) is just
this:

def multiply(a,b):
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c
Calling the program gives an error;
"david@palmer:~/source/python> ./test_String script1.py multiply 4 5
import went bang...
ImportError: No module named script1.py"
script1.py exists and it is in the same directory as the executable so its
not a path error or that kind of stuff.

wierd. does: http://www.python.org/doc/2.3.2/ext/pure-embedding.html work
for you ?

David
Jul 18 '05 #3
On Thu, 24 Mar 2005 21:12:18 +0000 David Harris wrote:

DH> int
DH> main(int argc, char *argv[])
DH> {
DH> PyObject *pName, *s, *pModule, *pDict, *pFunc;
DH> PyObject *pArgs, *pValue;
DH> int i;
DH>
DH> if (argc < 3) {
DH> fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
DH> return 1;
DH> }
DH>
DH>
DH> Py_Initialize();
[...]
DH> Calling the program gives an error;
DH> "david@palmer:~/source/python> ./test_String script1.py multiply 4
DH> 5 import went bang...
DH> ImportError: No module named script1.py"
DH> script1.py exists and it is in the same directory as the executable
DH> so its not a path error or that kind of stuff.

I believe you have to call Py_SetProgramName before Py_Initialize, or
otherwise let Python know that it should look for modules in this
directory. See http://python.org/doc/current/api/embedding.html#l2h-40

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 18 '05 #4
> wierd. does:
http://www.python.org/doc/2.3.*2/ext...embedding.html work
for you ?
Yes. It does.
./test_String script1.py multiply 4 5
Don't run it with the ".py" suffix. The argv[1] is a module name, not a
filename..

Even if you do, it may not find the module. Depending of what you have
in PYTHONPATH. Try also:

export PYTHONPATH=.
I believe you have to call Py_SetProgramName before Py_Initialize,


Yes. This is another way to affect the search path.

BranoZ

Jul 18 '05 #5
On Fri, 25 Mar 2005 11:09:35 +0300, Denis S. Otkidach wrote:
DH> Calling the program gives an error;
DH> "david@palmer:~/source/python> ./test_String script1.py multiply 4
DH> 5 import went bang...
DH> ImportError: No module named script1.py"
DH> script1.py exists and it is in the same directory as the executable
DH> so its not a path error or that kind of stuff.

I believe you have to call Py_SetProgramName before Py_Initialize, or
otherwise let Python know that it should look for modules in this
directory. See http://python.org/doc/current/api/embedding.html#l2h-40


excellent!! that was the tip (+ "PySys_SetArgv") the worked ...

This now works:

int
main(int argc, char *argv[])
{
PyObject *pName, *s, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;

Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
pName = PyString_FromString(argv[1]);
....
}

Thanks a bunch denis :)
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael R Seefelt | last post by:
I have written a simple C-program that loads a Python Function that connects to a PostgreSQL database> When I only load and execute the Python function all works OK. If I call the Python function a...
0
by: Adam McCarthy | last post by:
I'm trying to get a cross compiler working for arm-wince-pe. This is the output for the primes Pyrex example. If I compile simple Hello, World's etc, it works fine, but for some reason Python...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
3
by: Huayang Xia | last post by:
I get a python object by running a class' constructor. Then I need to modify the instance's attribute just like obj.attr1.attr2 = 'a' if in python's term. PyObject* py_obj_attr1 =...
1
by: Benke | last post by:
Hello, I'm quite new to Python and embedding python in c++. I'm trying to write a function that i can use to call a python function. It should take 3 arguments, the name of the python file, the...
1
by: jpw | last post by:
I am writing a Python / C++ embed app and it need to work on 3 platforms I have the PYTHONPATH variable set correctly and have gone back and downloaded compiled and installed the latest Python...
0
by: PlayDough | last post by:
I've embedded Python in an extension for a program we are using here at work. And I'm a bit stumped as to why I am getting an AttributeError only in the embedded Python. First, a bit of what I...
0
by: Riccardo Di Meo | last post by:
Hi everyone, I'm practicing with embedding python into C code and i have encountered a very strange problem: I'm unable to call the "accept" method of a (correctly created) server socket without...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.