Skip to content

Calling s_python_function_plot with Python error of no 'sys.argv[0]' #213

@YangRiverJiang

Description

@YangRiverJiang

Hi, I have encountered with an 'interesting' problem. I was running the minimal example (the plot-and-show simple code), and the line of calling matplotlib.pyplot.plot went into an error:
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args); // this is the 'plot' line

The captured error trace is obtained by PyErr_Print():
Traceback (most recent call last):
File "C:\Users\80575\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 2824, in plot
return gca().plot(
File "C:\Users\80575\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 2352, in gca
return gcf().gca(**kwargs)
File "C:\Users\80575\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 731, in gcf
return figure()
File "C:\Users\80575\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 677, in figure
**kwargs)
File "C:\Users\80575\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 299, in new_figure_manager
return backend_mod.new_figure_manager(*args, **kwargs)
File "C:\Users\80575\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backend_bases.py", line 3494, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File "C:\Users\80575\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends_backend_tk.py", line 868, in new_figure_manager_given_figure
window = tk.Tk(className="matplotlib")
File "C:\Users\80575\AppData\Local\Programs\Python\Python36\Lib\tkinter_init
.py", line 2012, in init
baseName = os.path.basename(sys.argv[0])
AttributeError: module 'sys' has no attribute 'argv'

This doesn't make sense because we set the program name as 'plotting' in default, which should be 'sys.argv[0]' on the Python side. I tried to call 'Py_GetProgramName' function before and after calling the 'plot' line, it shows 'plotting' before but something like '???' after it. Other strings like Py_GetBuildInfo() and Py_GetProgramFullPath() looks intact.
image

So I looked into each *.py file shown in the traceback, but they didn't modify sys.argv at all. My assumption of this problem is memory leaking.
Fortunately, I modified the code of Python36\Lib\tkinter_init_.py so it doesn't use sys.argv[0] anymore. Instead, I gave it a 'noname' constant string. Then my code worked.
I am not with a lot of Python background so I can hardly debug this further. It is worth noticing though.
My environment:
Visual Studio 2019 x64 Debug, Python 3.6, matplotlib-3.3.1

Yang

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions