ofSystem importing python modules

I am calling a python script from OF that needs a few modules and I am running into problems. There isn’t any issue when running the script from terminal.

I’m calling my python script with ofSystem and the absolute path. At first it couldn’t find any of the imported modules. I found that explicitly setting search paths with the following in my .py helped resolve a couple of the issues.

sys.path.append('/Users/carolinerecord/anaconda/lib/python3.6/site-packages')

But this doesn’t make a difference for importing other modules like portaudio (for example) even though it is also in the site-packages directory. It makes sense that portaudio would be different as it’s actually a c library being included via python. But I’m not sure how to resolve that! Any ideas?

I’m also having trouble importing the google cloud speech module.
I’ll try explicitly importing all the modules with their absolute paths next. But some of the modules are also including modules so I don’t think that will be the ultimate solution.

import importlib.util
spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py")
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
foo.MyClass()

I’ve seen this issue with system in that it doesn’t really have all your paths set, it’s kind of shell but more restricted – the easiest solution I found was to export path in the system command then run the python code. For example, from a recent project this line:

string command = "export PATH=\"..........\"; sh launch.sh"
system(command.c_str());

I’ve remove the path here since it’s super long basically what you get when you do

echo $PATH

at the command line.

you have to be careful about escaping quotes and spaces etc.

1 Like

Thanks Zach :+1:
It worked!

Exporting the path and then running the python file via its absolute path worked like a charm.

string command = "export PATH=\"..........\"; python /absolute/path/launch.py"
ofSystem(command);

right on! glad it’s working!