# Python launcher opens and immediately closes matplotlib.

**URL:** <https://mne.discourse.group/t/python-launcher-opens-and-immediately-closes-matplotlib/6066>\
**Category:** Support & Discussions\
**Tags:** eeg, visualization\
**Created:** [December 15, 2022, 1:12am UTC](https://mne.discourse.group/t/python-launcher-opens-and-immediately-closes-matplotlib/6066 "2022-12-15T01:12:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![willdecker](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/willdecker/32/2117_2.png) [@willdecker](https://mne.discourse.group/u/willdecker)\
**Post date:** [December 15, 2022, 1:12am UTC](https://mne.discourse.group/t/python-launcher-opens-and-immediately-closes-matplotlib/6066/1 "2022-12-15T01:12:17Z")

</div>

- MNE version: e.g. 0.24.0
- operating system: e.g. macOS 12

Here is the following code I am using

```python

# This script uses MNE to analyze and visualize neural time series data

import mne 
import numpy as np
import os
import matplotlib as plt

# Set the folder path
folder_path = '/path_to_file/'

# create an empty list to store the raw data
raw_data = []

# loop through the files in the folder
for filename in os.listdir(folder_path):
    # check if the file is a BrainVision file
    if filename.endswith(".vhdr"):
        # construct the full file path
        file_path = os.path.join(folder_path, filename)
        # load the data from the BrainVision file
        raw = mne.io.read_raw_brainvision(file_path)
        # add the raw data to the list
        raw_data.append(raw)

# combine the raw data into a single raw object
raw = mne.concatenate_raws(raw_data)

print(raw.info)

raw.plot_psd(fmax=50)
raw.plot(duration=5, n_channels=31)

```

Python launcher will open matplotlib but immediately close it. I am not sure what the problem could be. Could it be an issue with MNE dependancies?

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [December 15, 2022, 2:14pm UTC](https://mne.discourse.group/t/python-launcher-opens-and-immediately-closes-matplotlib/6066/2 "2022-12-15T14:14:38Z")

</div>

Hello and welcome to the forum!

I believe the following posting answers your question:

> [@Why does using a different IDE affect my raw.plot()?](https://mne.discourse.group/t/why-does-using-a-different-ide-affect-my-raw-plot/2770/2):
>
> Hello, The Python interpreter closes immediately after opening the plot window, because the end of your script is reached. You have two ways to work around this: Ensure execution is halted after plotting by passing block=True, i.e. raw.plot(block=True) Or edit your PyCharm run configuration such that your script is run in interactive mode (which might be something you want when working with data interactively anyway!) by ticking the “Run with Python Console” box. See [Create and edit run/debu…](https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html)

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [December 15, 2022, 2:50pm UTC](https://mne.discourse.group/t/python-launcher-opens-and-immediately-closes-matplotlib/6066/3 "2022-12-15T14:50:15Z")

</div>

The answer @richard linked to may be a bit confusing since it’s not clear that you’re using PyCharm (as that asker was). The gist of the answer is the same though: Python can run in two ways: interactive and non-interactive mode. From the command line, this is controlled by the `-i` flag:

```python
$ python myscript.py # non-interactive
$ python -i myscript.py # interactive

```

in interactive mode, when the end of the script is reached, the python interpreter _stays open_ and lets you enter additional commands. A side-effect of this is that any plots that your script opened will also stay open.

If you’re running your script through an IDE, it might automatically use interactive mode, or you may need to look around in the commands / settings for a way to tell it which mode to use when running scripts.

As mentioned in the linked answer, another option is using the MNE-Python plotting function option `block=True`, which pauses script execution while that plot is open, and continues with the script only after you manually close the plot. However, not all plotting functions have this option (`raw.plot_psd()` does not; `raw.plot()` does) so it won’t always be enough.
