Launch Python script from Juce

Hi, in my project I’d like to run this simple code:

#include <Python.h>

MainComponent::MainComponent()
{
    setSize (600, 400);

    File file(File::getSpecialLocation(File::userHomeDirectory));
    File mainPy = file.getChildFile("PycharmProjects/pythonProject/main.py");

    FILE* pyFile;
    Py_Initialize();
    PyRun_SimpleFile(pyFile, mainPy.getFullPathName().toRawUTF8());
}

In projucer project I put this line inside “Header Search Path” under exporters:

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/Headers/

But When I compile XCode gives me this error:

Undefined symbols for architecture x86_64:
"_PyRun_SimpleFileExFlags", referenced from:
  MainComponent::MainComponent() in MainComponent.o
"_Py_Initialize", referenced from:
  MainComponent::MainComponent() in MainComponent.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is a linker error, you can see this as it’s generated by ld which is the linker used by Xcode. Likely reason: you did not link to the library itself.

If it’s not explicitly a header only library, you not only need to specify where to find the header as you did, but also need to link to the library file itself. The header is just some piece of information on what functions the compiler can expect to exist somewhere.

The linker then has to make sure that it finds the places where the actual functionality is defined. This can be a static library (.a file on macOS) which will be completely made a part of your binary or a dynamic library (.dylib file on macOS) which you need to supply with the application in order to run it somewhere else then on your own machine.

As it is obviously a .framework you are working with, it’s even easier as the .framework format is an Apple specific way of setting all this up in one step. So it probably should just work to specify Python3 under the extra system frameworks field in the Xcode exporter in the Projucer.

2 Likes

Really thank you! but trying to add this in “Extra Library Search Path” of projucer xCode doesn’t find <Python.h>:

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework

Have you simply tried adding Python3 to that field, without the path? :slightly_smiling_face:

1 Like

And you should add it to the extra frameworks field

2 Likes

really thank you, but I don’t find this extra frameworks field… I wrote Python3 in Extra System Frameworks, but compiler says me that he don’t find Python.h

can you do a quick screenshot of your projucer ? (sorry I seem stupid, but I don’t find it :pray:t2: )

Nowadays the field is named “Extra Custom Frameworks”, and it is not far from “Extra System Frameworks” in Projucer’s UI.

1 Like

ok, perfect I added: /System/Library/Frameworks/Python.framework
In that field and Included in my MainComponent.h <Python/Python.h>, now it succed!

Thank you All!

Hi, sorry, it’s me again :pray:t2:

I correctly embed python on my project and all works, I can run a .py file or run code passed by string, but in .py files I need to run I’ve imported some third party libraries (as selenium-webdriver or pyautogui) and now when I try to run those files my py_instance is not able to find them (obviously…)

this is what console tells me:

Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named selenium

I wrote these files using PyCharm and there as you know there to include them I wrote pip install selenium or pip install pyautogui, here how can I achieve this?

Ok sorry my mistake, simply it was not installed!
I coded my file.py use PyCharm and I called pip install selenium from PyCharm terminal not from Mac terminal