How Can I run some Python Scripts in VS C++?

B

bing

Hi, I have some simple python scripts, anyone knows how to run the
Python Scripts in C++?
Any code example would be really helpful and appreciated.
 
G

Gabriel Genellina

Hi, I have some simple python scripts, anyone knows how to run the
Python Scripts in C++?
Any code example would be really helpful and appreciated.

Do you want to write a C++ application, and allow your users to write
scripts in Python, possibly exposing some objects from you application?

Yes: read the "Extending and Embedding" document, specially the "Embedding
part" (last)
<http://docs.python.org/extending/index.html>

No: please explain in more detail what you want to do.
 
D

david

No: please explain in more detail what you want to do.

Thanks for the fast reply Gabriel,
Basically I have some python scripts to do some document processing,
all in command line tho.
I want to have an C++ application so that my scripts can run in dialogs
(API).
I saw a post before using c# to run python scripts within the c# main.
I am willing to run my python scripts within c++.
Thanks.
 
G

Gabriel Genellina

Basically I have some python scripts to do some document processing,
all in command line tho.
I want to have an C++ application so that my scripts can run in dialogs
(API).
I saw a post before using c# to run python scripts within the c# main.
I am willing to run my python scripts within c++.
Thanks.

So you want to execute a Python script, in a different process, as if you
typed the command line?
That's not different to executing any other external program in C# - in
fact is a C# question, no Python involved. Try something like this:

Process p = new Process();
p.StartInfo.FileName = "path\to\python.exe";
p.StartInfo.Arguments = "path\to\your\script.py other arguments";
p.Start();
p.WaitForExit();
p.Close()

There are other properties you may want to use, like
RedirectStandardOutput; see the C# documentation.
 
D

david

So you want to execute a Python script, in a different process, as if you  
typed the command line?
That's not different to executing any other external program in C# - in  
fact is a C# question, no Python involved. Try something like this:

     Process p = new Process();
     p.StartInfo.FileName = "path\to\python.exe";
     p.StartInfo.Arguments = "path\to\your\script.py other arguments";
     p.Start();
     p.WaitForExit();
     p.Close()

There are other properties you may want to use, like  
RedirectStandardOutput; see the C# documentation.

Thanks so much. I'll give it a try :)
Best Regards.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,525
Members
48,249
Latest member
reactnativeexpert

Latest Threads

Top