1

I have a python script which is invoked inside a C++ application. When I import matplotlib I get the following error.

List index out of range.

In my script the only matplotlibrelated code is

import matplotlib.pyplot as plt
f = plt.figure()

When I invoke the script on its own it works. I get this exception only when it is embedded in a C++ Program. Do I need to provide some extra dependencies when the script uses matplotlib?

When I comment out the matplotlibfunctions I don't get the exception

Update

The C++ Code is from the python docs link : 1.3. Pure Embedding

I haven't modified anything from the C++ code. It works as long as there are no matplotlib related code.

4
  • can you provide a minimal reproducible example ? Commented Dec 10, 2019 at 9:42
  • how do you embed it in c++? Commented Dec 10, 2019 at 9:43
  • updated the post. Commented Dec 10, 2019 at 9:45
  • 1.3 Pure Embedding I have mentioned that in the post. Commented Dec 10, 2019 at 9:47

1 Answer 1

1

The reason is that the backend used by matplotlib is tk in my case. The window initialization of the tk has this code in there

baseName = os.path.basename(sys.argv[0]) 

And in my case the argv is empty and that is the reason for list index out of range error.

One work around I found here. We can passing dummy values like this:

    wchar_t const *dummy_args[] = { L"Python", NULL };  // const is needed because literals must not be modified
    wchar_t const **argv = dummy_args;
    int             argc = sizeof(dummy_args) / sizeof(dummy_args[0]) - 1;
    PySys_SetArgv(argc, const_cast<wchar_t **>(argv));
Sign up to request clarification or add additional context in comments.

Comments

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.