-1

I am a composition student, and I am developing py4pd https://github.com/charlesneimog/py4pd, a code that can run Python with Puredata.

One of the problems is that I currently need a Python installation for the code to load. My question: is it possible to use the Python/C API without having to install Python? It would be much easier for people who don't understand code (and stuff) and terminal to use the tool (On all platforms, mainly Windows for now).

Thank you very much!

  • Just ship the Python DLL with your project (licensing permitting). – Charles Duffy Aug 11 '22 at 22:44
  • I already try it, It does not work! – Charles K. Neimog Aug 11 '22 at 22:45
  • 1
    Mind, you'll also need any actual Python-language _libraries_ that code being run depends on. Finding them and bundling them up is a big part of what tools like pyinstaller do. But without being told exactly what happened when you tried, we don't know if that's the issue or not. – Charles Duffy Aug 11 '22 at 22:45
  • 2
    "do[es] not work" is not a specific enough problem description that we can help you fix it. We need to know what went wrong in enough detail to recognize the problem and reproduce it ourselves to test proposed fixes. – Charles Duffy Aug 11 '22 at 22:45
  • 1
    No, you need Python to use Python. But maybe you can include a copy inside py4pd so that people don't need to install it separately. – user253751 Aug 11 '22 at 22:51
  • Sorry. But I do not understand the problem because I have no error in the console of PureData, and all the Puredata crashes. – Charles K. Neimog Aug 11 '22 at 22:53
  • I was thinking that the problem was the libraries, but when I try to run the function with just print, it crashes too. – Charles K. Neimog Aug 11 '22 at 22:57
  • The problem with including Python with py4pd is that I couldn't change where the Python API looks for Python. I saw in the documentation how this could be done, but in my code, it doesn't work. – Charles K. Neimog Aug 11 '22 at 23:00
  • @CharlesK.Neimog The API is something you can use once Python (the DLL) is already loaded into your program - hopefully your compiler did that automatically - you'd want to look up how to load a DLL from a specific place. And how to set `PYTHONPATH` (the place it loads all the .py files from) – user253751 Aug 12 '22 at 00:26
  • @user253751 this is already possible. My problem is that Python needs some of the files from the DLLs folder, and it always looks it in the installation folder. But I think that I found the solution. It is not possible overwrite the Python Paths (for DLLs) in the programming (If I understood correctly). In the same place that the python310.dll, you need to create a file called {python_dll_name._pth} and put there, one by line, the folders where Python will look for addicional folder, in my case, it needs just of the folder DLLs and Lib. Thanks for the help! – Charles K. Neimog Aug 12 '22 at 13:16

2 Answers2

1

It is impossible to overwrite the Python Paths (for DLLs) in the programming (If I understood correctly). In the same place as the python310.dll, you need to create a file called {python_dll_name}._pth and put there, one by line, the folders where Python will look for the additional folder. In my case, it needs just the folder DLLs and Lib. Here is an example of my python310._pth.

./Python/Lib
./Python/DLLs

Now it works without the installation!

DOC: https://docs.python.org/3/using/windows.html topic 4.9.

0

One can embed python. A simple example follows:

#define PY_SSIZE_T_CLEAN
#include <Python.h>

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

One would compile this, using gcc (or clang):

$(CC) /opt/bin/python3.4-config --cflags # where CC is an environment variable pointing to wherever the compiler used to build python is located.

In windows, there's a video and explanation here, but I'm not familiar with the Microsoft ecosystem, so cannot offer more advice.

hd1
  • 33,938
  • 5
  • 80
  • 91