473,480 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

python commmand line params from c++

What is the proper way to pass command line parameters to a python
script called from C++? I'm tryng this:

path = "c:\\someDir\\someScript.py param1 param2 param3";
PyRun_SimpleFile(PyFile_AsFile( PyFile_FromString( path, "r")),
"someScript.py");

I'm getting a "format error someScript.py, line 1" when the code is
executed.

Note: The strange appearannce of the 3 python function calls nested is
not a bug, but required to prevent a run-time error.

Jul 19 '05 #1
4 2002
On 30 Jun 2005 07:52:04 -0700
"Wesley Henwood" <we***********@hotmail.com> wrote:
What is the proper way to pass command line parameters to a python
script called from C++? I'm tryng this:
Have you tried PySys_SetArgv?
path = "c:\\someDir\\someScript.py param1 param2 param3";
PyRun_SimpleFile(PyFile_AsFile( PyFile_FromString( path, "r")),
"someScript.py");
This code looks strange: you open file and create Python file object
from its descriptor (PyFile_FromString), then get descripto back
(PyFile_AsFile) to pass it to PyRun_SimpleFile. Why don't you just use
C fopen function?
I'm getting a "format error someScript.py, line 1" when the code is
executed.

Note: The strange appearannce of the 3 python function calls nested is
not a bug, but required to prevent a run-time error.


I believe it's due to PyFile_FromString failing to open non-existent
file. You must check return value of it.

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 19 '05 #2
Thanks Denis. PySys_SetArgv should do the trick. The nested function
calls are there because the FILE structrure produced by fopen is not
always compatible with the FILE structure produced by the Python C++
functions. Something do with the Python C being built with different
version ofthe c run-time libs.

The problem is a bit strange and should be adressed, it's documented
somewhere on the Python site, but no solution is given. That I had to
find by trial and error. Even better would be a function such as
PyRun_File(char *filename), and do away with handling the FILE
structure altogether.

Jul 19 '05 #3
ej
Ummm, just a guess from looking at the output (not familiar with C++
interface)...
the error seems to complain about what's in someScript.py, not the call to
it.
What's in someScript.py?
"Wesley Henwood" <we***********@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
What is the proper way to pass command line parameters to a python
script called from C++? I'm tryng this:

path = "c:\\someDir\\someScript.py param1 param2 param3";
PyRun_SimpleFile(PyFile_AsFile( PyFile_FromString( path, "r")),
"someScript.py");

I'm getting a "format error someScript.py, line 1" when the code is
executed.

Note: The strange appearannce of the 3 python function calls nested is
not a bug, but required to prevent a run-time error.

Jul 19 '05 #4
Thanks Denis. PySys_SetArgv should do the trick. The nested function
calls are there because the FILE structrure produced by fopen is not
always compatible with the FILE structure produced by the Python C++
functions. Something do with the Python C being built with different
version ofthe c run-time libs.
The FILE problem is a bit strange and should be adressed, it's
documented
somewhere on the Python site, but no solution is given. That I had to
find by trial and error. Even better would be a function such as
PyRun_File(char *filename), and do away with handling the FILE
structure altogether.

Jul 19 '05 #5

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

Similar topics

5
2010
by: Magnus Lyck? | last post by:
Something really strange is happening to me (sometimes). I'm using Python 2.3.2 on NT 4.0 as well as win32all-157, adodbapi and db_row. During a recursive call to a method, it seems Python...
16
1498
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.4. Python 2.4 is a final, stable release, and we can recommend that Python users...
4
1762
by: pmcgover | last post by:
I enjoyed Paul Barry's September article in Linux Journal entitled, "Web Reporting with MySQL, CSS and Perl". It provides a simple, elegant way to use HTML to display database content without any...
0
1408
by: Nathan Harmston | last post by:
HI, I posted this to sqlalchemy but didnt get a response, so I was wondering if anyone on python-list could help me. I m currently trying to build an api for a database and I really like the...
0
7039
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
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7080
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
6895
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...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.