Calling a python script from of

Hello , i have 2 questions, first is how can i run a python script from a of app?

my python script downloads pictures from google images to my data folder of my openframeworks app. Do you think its a good idea to call the python script when i start my of app, or is there any advantaje if i remake my python script into c++ openframeworks, so i just have one openframeworks app and i dont need to start the python script from my main openframeworks app?
which are the differences between these 2 approaches , and what are the benefits ?

thanks

Matias

you can run anything from OF and c in general by using:

system(“command”);

but you can also use ofxHttpUtils to download anything, i think there’s even some modification at some place by jesus gollonet to directly load images in an ofImage from a stream.

i have a version of ofxHttpUtils here:

http://github.com/arturoc/ofxHttpUtils

the pros of doing everything in OF is that you’ll have more control over what’s happening like if something goes wrong.

the pros of using your already done python script is that it’s already done : )

i suppose the best approach is to try with your script first. if that works ok for you, then let it as it is, if you find problems then reimplement it in OF

hi arturo ive tried the command system but it doesnt work. do i need to load a special library for using it?

ive tried system(“python my_script.py”) but nothing happens, and when i try

system(python my_script.py); i get error variable python doesnt exist.

How can i use that command?

thanks

Matias

system(“python my_script.py”) is the correct format. You just need to make sure my_script.py is in the bin folder of your of app’s directory. alternatively you can fully qualify the location, such as system(“python /home/tim/scripts/my_script.py”)

Hope this helps

Hi Tim, ive tried both approaches :
system(“python /Users/topo/Desktop/of/of_preRelease_v0.06_xcode_FAT/apps/examples/my_app/bin/dowload_pics.py”);

and

system(“python download_pics.py”);

but they do anything!!

I dont know why this doesnt work here!! im on macosx leopard using of v.0.06

im also calling that in my setup function, any idea of what can be wrong?

maybe do i need to load a library?

thanks

Take a look at this page http://docs.python.org/c-api/intro.html-…-ing-python
It works almost out-of-the box if you’re fimmiliar with Python
Cheers!

I’m having the same problem as well. My python script is in my bin folder, and I don’t get any errors to suggest that the system() function can’t find/run my script, and yet nothing is happening. How were you able to resolve this?

I’m trying to read that link to the python documentation but I’m not sure if I understand it :slight_smile:

The system call should work. Maybe try putting the python script in your data folder and call it with system("python "+ofToDataPath(“scriptname.py”, true));

This is a bit late… but:
you need to convert the string into a c_str:

string mySweetPythonCommand = "python doStuff.py";
system(mySweetPythonCommand.c_str());

What i’m not sure is… when i run python from openframeworks using this method… it doesn’t seem to read my filepaths correctly. Anyone know anything about this?

What filepaths do you mean? Does it find your python script but not the libraries? Or can’t the script be found?

btw you can also do https://docs.python.org/2/extending/embedding.html#very-high-level-embedding

Thanks for replying!
The script can be found, but i have a filepath within the script that didn’t seem to work. I wasn’t sure if I should post this here as it may be a python issue about being called using system()… but here we go anyway:

So in my OF project, I have the following:

string mySweetPythonCommand = "python doStuff.py";
system(mySweetPythonCommand.c_str());

And in my doStuff.py script, I have the following:

textfile = open("folder/filename.txt")

When I run the python script from Terminal the text file is opened correctly. However, when I run the script from within openframeworks using the system command, the script is executed, but it cannot seem to find the text file.

I have found a workaround whereby I need to enter the entire filepath of the textfile in the python script:

filepath = "/Users/myUsername/Documents/Github/openFrameworks/apps/myProject/bin/data/folder/filename.txt")
textfile = open(filepath)

But this is not ideal as I may need to have this applicable to whatever folder the script is in. Any hints?

Yes, check this again

I’m having the same problem as @cherdd, in order to get the script to run I have:

string command = "python "+ofToDataPath("script.py", true);
system(command.c_str());

The script is in my data folder. This is the only configuration of the above that I can get to run. However, the script cannot find files in it’s own folder, even though it has no problem doing so when run from the terminal. What’s especially interesting is the script IS able to run code from other scripts in the data folder that it has imported using statements like:

import OtherScript

The problem goes away if I re-write the paths to text files as absolute (as @cherdd did), but this is obviously not ideal. I also have the strange behavior where when I try to bind a socket to receive messages from the oF patch to the python script it claims:

socket.error: [Errno 48] Address already in use

This happens no matter what port I put the socket to listen to. It works perfectly fine if I simply start the oF app (without the system call) and then run the python script from the terminal.

the working directory of something run using system or ofSystem in OF is the bin folder or in osx some place inside the app package.

perhaps you could pass a path as parameter to the script and use that to create the paths, where the path would be ofToDataPath("")

This is working well for me, thanks.

Hi all,
new blood to old topic…
i do not want to run a script but just start another program in my app.
To be more precise, i want to start a pure data patch in an OF app right after the OF app runs. They should then run simultaneously until i terminate both.
i couldn’t figure out how i should use the ofSystem function.
i found an addon “ofxPd” but it looks like it is not to run a patch in an OF app but to call some functionalities from it. Or did i get it wrong?
Is there a simple, path = C:… start.this command ?:slight_smile: