Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i'm trying to develop a dll that embeds python. I am using boost 1.72 and python 3.7

The dll exports some function to initialize python, adds some numpy array and launches a script. I created a VBA macro in excel that calls the dll function.

the function are:

C++
long __stdcall Init(); //initialize python
long __stdcall AddInput(double* data, long nRows, long nCols, const char* label); //adds input
long __stdcall ExecFile(const char* file); //execs a python script
void __stdcall Clear();


everything works fine as long as I don't import matplotlib.
Infact if I try to import matplotlib.pyplot in python script and plot an array an exception is thrown.
How can I use matplotlib.pyplot in script called from a dll embedding python?


the script i'm trying to exec is

Python
import numpy as np
import matplotlib.pyplot as plt
import sys


def main():
	try:
		sys.stdout = open("outPy.log","w")
		
		plt.plot([1,2,4],[1,2,3])
		plt.show()
	except:
		print("Unexpected error:", sys.exc_info()[0])
		print("Unexpected error:", sys.exc_info()[1])
		print("Unexpected error:", sys.exc_info()[2])
	finally:
		sys.stdout.close()
	
	

if __name__ == '__main__':
   main()


the exception is:
Unexpected error: <class 'IndexError'>
Unexpected error: list index out of range
Unexpected error: <traceback object at 0x2061C030>


What I have tried:

Is this because I'am calling the dll from VBA?

Is it a question of thread?
Posted
Updated 1-Apr-20 0:06am
v2
Comments
Richard MacCutchan 1-Apr-20 6:51am    
You need to identify which line of code throws the exception. Then which index value is out of range.
Optimistic76 1-Apr-20 7:33am    
the line is

plt.plot([1,2,4],[1,2,3])
Optimistic76 1-Apr-20 7:53am    
Eureka!! Richard your suggestion make me think and i found that i didn't call PySys_setArg in python initialization!!!

I just added in the script
if len(sys.argv) < 1:
sys.argv.append("")
and everything works fine!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900