OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Randomly getting "ValueError: PyCapsule_New called with null pointer" when making plots with Python 3.12 fresh install on new PC

  • Thread starter Thread starter barriboy
  • Start date Start date
B

barriboy

Guest
I use a suite of python 3.7 code for my work, generating various plots and fitting lines. It works perfectly fine on my old windows 10 machine.

However, I just got a new laptop and installed 3.12 on it. The code still works, but SOMETIMES it randomly decides to fail on a plot, giving "ValueError: PyCapsule_New called with null pointer" originating from a simple line of code that I use to initialize new plots: plt.figure(figsize=(figx,figy)) #generate clean figure.

I can see no rhyme or reason as to why it sometimes works and occasionally fails. In a few instances, I've literally just changed the order of certain plots and it works just fine. In others, I change minor details like the initial guess for the center of a gaussian for a line fit (by one index), and it works just fine all of a sudden.

I'm at a loss as to how to move forward with this, as the problem seems to be some random, arcane issue buried deep within matplotlib or python or something, clearly relating to something with the fresh install on my new machine.

Any suggestions?

Full error below:

Code:
File "D:\astra\ASTRA MS\Massify.py", line 435, in <module>
    ASTRA_MS.plot_set(x_m,t,y_ms,y_blur,y_superblur,y_megablur,figtext,titletext,shotnum,plotstyle=plotstyle,savepath=path,falling=falling,vlines=vlines,vacc=vacc,xlim=xlim,showplots=showplots,invertplot=invertplot,metadata=str(values))
  File "D:\astra\ASTRA MS\ASTRA_MS.py", line 259, in plot_set
    plt.figure(figsize=(figx,figy)) #generate clean figure
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 1022, in figure
    manager = new_figure_manager(
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 545, in new_figure_manager
    return _get_backend_mod().new_figure_manager(*args, **kwargs)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3521, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3526, in new_figure_manager_given_figure
    return cls.FigureCanvas.new_manager(figure, num)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 1811, in new_manager
    return cls.manager_class.create_with_canvas(cls, figure, num)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 479, in create_with_canvas
    with _restore_foreground_window_at_end():
  File "C:\Program Files\Python312\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 43, in _restore_foreground_window_at_end
    foreground = _c_internal_utils.Win32_GetForegroundWindow()
ValueError: PyCapsule_New called with null pointer
<p>I use a suite of python 3.7 code for my work, generating various plots and fitting lines. It works perfectly fine on my old windows 10 machine.</p>
<p>However, I just got a new laptop and installed 3.12 on it. The code still works, but SOMETIMES it randomly decides to fail on a plot, giving "ValueError: PyCapsule_New called with null pointer" originating from a simple line of code that I use to initialize new plots: <code>plt.figure(figsize=(figx,figy)) #generate clean figure</code>.</p>
<p>I can see no rhyme or reason as to why it sometimes works and occasionally fails. In a few instances, I've literally just changed the order of certain plots and it works just fine. In others, I change minor details like the initial guess for the center of a gaussian for a line fit (by one index), and it works just fine all of a sudden.</p>
<p>I'm at a loss as to how to move forward with this, as the problem seems to be some random, arcane issue buried deep within matplotlib or python or something, clearly relating to something with the fresh install on my new machine.</p>
<p>Any suggestions?</p>
<p>Full error below:</p>
<pre><code>File "D:\astra\ASTRA MS\Massify.py", line 435, in <module>
ASTRA_MS.plot_set(x_m,t,y_ms,y_blur,y_superblur,y_megablur,figtext,titletext,shotnum,plotstyle=plotstyle,savepath=path,falling=falling,vlines=vlines,vacc=vacc,xlim=xlim,showplots=showplots,invertplot=invertplot,metadata=str(values))
File "D:\astra\ASTRA MS\ASTRA_MS.py", line 259, in plot_set
plt.figure(figsize=(figx,figy)) #generate clean figure
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 1022, in figure
manager = new_figure_manager(
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 545, in new_figure_manager
return _get_backend_mod().new_figure_manager(*args, **kwargs)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3521, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3526, in new_figure_manager_given_figure
return cls.FigureCanvas.new_manager(figure, num)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 1811, in new_manager
return cls.manager_class.create_with_canvas(cls, figure, num)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 479, in create_with_canvas
with _restore_foreground_window_at_end():
File "C:\Program Files\Python312\Lib\contextlib.py", line 137, in __enter__
return next(self.gen)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 43, in _restore_foreground_window_at_end
foreground = _c_internal_utils.Win32_GetForegroundWindow()
ValueError: PyCapsule_New called with null pointer
</code></pre>
 
I use a suite of python 3.7 code for my work, generating various plots and fitting lines. It works perfectly fine on my old windows 10 machine.

However, I just got a new laptop and installed 3.12 on it. The code still works, but SOMETIMES it randomly decides to fail on a plot, giving "ValueError: PyCapsule_New called with null pointer" originating from a simple line of code that I use to initialize new plots: plt.figure(figsize=(figx,figy)) #generate clean figure.

I can see no rhyme or reason as to why it sometimes works and occasionally fails. In a few instances, I've literally just changed the order of certain plots and it works just fine. In others, I change minor details like the initial guess for the center of a gaussian for a line fit (by one index), and it works just fine all of a sudden.

I'm at a loss as to how to move forward with this, as the problem seems to be some random, arcane issue buried deep within matplotlib or python or something, clearly relating to something with the fresh install on my new machine.

Any suggestions?

Full error below:

Code:
File "D:\astra\ASTRA MS\Massify.py", line 435, in <module>
    ASTRA_MS.plot_set(x_m,t,y_ms,y_blur,y_superblur,y_megablur,figtext,titletext,shotnum,plotstyle=plotstyle,savepath=path,falling=falling,vlines=vlines,vacc=vacc,xlim=xlim,showplots=showplots,invertplot=invertplot,metadata=str(values))
  File "D:\astra\ASTRA MS\ASTRA_MS.py", line 259, in plot_set
    plt.figure(figsize=(figx,figy)) #generate clean figure
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 1022, in figure
    manager = new_figure_manager(
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 545, in new_figure_manager
    return _get_backend_mod().new_figure_manager(*args, **kwargs)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3521, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3526, in new_figure_manager_given_figure
    return cls.FigureCanvas.new_manager(figure, num)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 1811, in new_manager
    return cls.manager_class.create_with_canvas(cls, figure, num)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 479, in create_with_canvas
    with _restore_foreground_window_at_end():
  File "C:\Program Files\Python312\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
  File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 43, in _restore_foreground_window_at_end
    foreground = _c_internal_utils.Win32_GetForegroundWindow()
ValueError: PyCapsule_New called with null pointer
<p>I use a suite of python 3.7 code for my work, generating various plots and fitting lines. It works perfectly fine on my old windows 10 machine.</p>
<p>However, I just got a new laptop and installed 3.12 on it. The code still works, but SOMETIMES it randomly decides to fail on a plot, giving "ValueError: PyCapsule_New called with null pointer" originating from a simple line of code that I use to initialize new plots: <code>plt.figure(figsize=(figx,figy)) #generate clean figure</code>.</p>
<p>I can see no rhyme or reason as to why it sometimes works and occasionally fails. In a few instances, I've literally just changed the order of certain plots and it works just fine. In others, I change minor details like the initial guess for the center of a gaussian for a line fit (by one index), and it works just fine all of a sudden.</p>
<p>I'm at a loss as to how to move forward with this, as the problem seems to be some random, arcane issue buried deep within matplotlib or python or something, clearly relating to something with the fresh install on my new machine.</p>
<p>Any suggestions?</p>
<p>Full error below:</p>
<pre><code>File "D:\astra\ASTRA MS\Massify.py", line 435, in <module>
ASTRA_MS.plot_set(x_m,t,y_ms,y_blur,y_superblur,y_megablur,figtext,titletext,shotnum,plotstyle=plotstyle,savepath=path,falling=falling,vlines=vlines,vacc=vacc,xlim=xlim,showplots=showplots,invertplot=invertplot,metadata=str(values))
File "D:\astra\ASTRA MS\ASTRA_MS.py", line 259, in plot_set
plt.figure(figsize=(figx,figy)) #generate clean figure
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 1022, in figure
manager = new_figure_manager(
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 545, in new_figure_manager
return _get_backend_mod().new_figure_manager(*args, **kwargs)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3521, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3526, in new_figure_manager_given_figure
return cls.FigureCanvas.new_manager(figure, num)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 1811, in new_manager
return cls.manager_class.create_with_canvas(cls, figure, num)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 479, in create_with_canvas
with _restore_foreground_window_at_end():
File "C:\Program Files\Python312\Lib\contextlib.py", line 137, in __enter__
return next(self.gen)
File "C:\Users\zachu\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 43, in _restore_foreground_window_at_end
foreground = _c_internal_utils.Win32_GetForegroundWindow()
ValueError: PyCapsule_New called with null pointer
</code></pre>
The error `ValueError: PyCapsule_New called with null pointer` typically occurs due to issues with the matplotlib backend, especially when using the Tkinter backend (`TkAgg`). This can be exacerbated by different versions of Python or incompatible dependencies on your new laptop.

Here are some steps you can take to troubleshoot and potentially resolve the issue:

1. **Update matplotlib**:
- Ensure that matplotlib is up-to-date on your new laptop. Sometimes, updating to the latest version can resolve compatibility issues:
```
pip install --upgrade matplotlib
```

2. **Check Tkinter Installation**:
- Verify that Tkinter is installed and properly configured with your Python environment. Tkinter is often included with Python installations, but it's worth checking:
```
python -m tkinter
```

3. **Check Python Version Compatibility**:
- Matplotlib and its backend dependencies can be sensitive to Python versions. Since you're using Python 3.12, ensure that all packages, including matplotlib and Tkinter, are compatible with this version.

4. **Change Matplotlib Backend**:
- Try switching the matplotlib backend to a different one that might be more stable on your new laptop. You can set the backend before importing pyplot:
```python
import matplotlib
matplotlib.use('Qt5Agg') # or 'Agg', 'TkAgg', etc.
import matplotlib.pyplot as plt
```
- Experiment with different backends (`Qt5Agg`, `Agg`, `TkAgg`, etc.) to see if the issue persists.

5. **Environment Cleanup and Reinstallation**:
- Sometimes, issues can be resolved by cleaning up the Python environment and reinstalling necessary packages:
```
pip uninstall matplotlib
pip install matplotlib
```

6. **Check System Dependencies**:
- Verify that all necessary system dependencies for Tkinter and matplotlib are installed on your new laptop. This might involve checking for system updates or additional libraries required by Tkinter.

7. **Run in a Virtual Environment**:
- Create a virtual environment specifically for your project and install all dependencies within that environment. This ensures that dependencies are isolated and don't conflict with system-wide installations.

8. **Consult Matplotlib Documentation**:
- Check the [matplotlib documentation](https://matplotlib.org/stable/users/installing.html) for any specific troubleshooting steps related to your version of Python or operating system.

By following these steps, you should be able to diagnose and resolve the `PyCapsule_New` error with matplotlib when plotting in your Python 3.12 environment on your new laptop. If the issue persists, trying different backend configurations or seeking further assistance from the matplotlib community forums or GitHub issues page might be necessary.
 
Top