4

I am currently working on a C++ gui application. The application uses the Python/C API to call some python scripts. The scripts are located in the solution directory, and I call them by simply providing the path. This is currently working fine while debugging the application or even running the generated .exe file, but I am wondering how this could work if I want to release and distribute the application onto a different computer for someone to use. How can these scripts be deployed with the application?

I also have a .ttf font file with the same situation. How can this resource file be deployed with the application?

In other words, I want to deploy/release a C++ application with the scripts and resource files.

FYI: the C++ application is a Visual Studio project.

Thanks for the help in advance, and let me know if any more information is needed!

Update:

I just wanted to clear up the way my project is working currently:

PyObject* pArgs = PyTuple_New(5); // I setup the arguments the python function needs
PyImport_ImportModule("requests"); // imports...
// make python call
PyObject* pResult = PyObject_CallObject(pFunc, pArgs);

So this is (for the most part) how I call the scripts with the C++ source code. The scripts are located in a folder that is located in the solution directory.

I hope this explains my problem a little better.

Update:

Just another little update... Using some answers to other similar questions got me to the following point:

I need to obtain a python library, compile and link it with my C++ application, and then bundle the dependencies with the application (How to distribute C++ application which calls Python?)

So I guess my question is now shifting to how I would be able to get this done. What are the specific steps to do this? I also got a link (https://docs.python.org/3.5/using/windows.html#embedded-distribution) to an embedded distribution of a python environment (maybe this should somehow be used?). Also, I am able to statically link python into the application. I just don't know how to bundle and deploy the scripts I created and use in the application.

12
  • if youre looking for a py->exe converter, pyinstaller works. You can then just implement an argparser related module to call your exe from your c++ file. Commented Jun 9, 2022 at 19:09
  • Are you looking for a software installer? Commented Jun 9, 2022 at 19:51
  • I don't have a problem running the scripts with C++ source code (that's what the API is for). I'm just unsure how the scripts can be packed with the application if I want to deploy/release the application. Commented Jun 9, 2022 at 22:57
  • Are you using an absolute path or a relative path? Commented Jun 10, 2022 at 2:21
  • 1
    I see, thank you so much! @levente.nas . I will provide you an answer if I arrive before to this point, I already integrated the Python C API in the client. I miss generating the DLL. I think this need to be done with Cython. shorturl.at/dqDT2 "A .pyx or .py file is compiled by Cython to a .c file, containing the code of a Python extension module. The .c file is compiled by a C compiler to a .so file (or .pyd on Windows) which can be import-ed directly into a Python session. setuptools takes care of this part. Although Cython can call them for you in certain cases" Commented Oct 20, 2022 at 11:53

2 Answers 2

4
+50
PyImport_ImportModule("requests")

The parameter is "requests". Put the py file aside exe file when distributing.

Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to do this with all the python libraries the application uses?
Libraries usually also need to be placed in the same directory as exe or in the system environment path.
1

So, you need to make sure that the C++ application can still access the python libraries when its released and those libraries/dependencies arent necessarily available on other systems.

You'll need to, like another commenter suggested, use one of the importing modules utilities, like PyImport_ImportModule("library name").

You can see these utilities here: https://docs.python.org/3/c-api/import.html

You'll also need to either

  1. Put the libraries that you want with the exe (in the same directory) or
  2. put them in the system environment path ( which is probably less straightforward).

Hope that helps and that I understood you're question correctly.

1 Comment

Yes, that's helpful. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.