title: Python script has to be run twice to execute
body: <p>I have written a python script to gather and analyze data from a <code>.csv</code> file and then plot it using the <code>matplotlib.pyplot</code> module. I'm using <code>numpy.genfromtext()</code> to gather the data.</p>
  
  <p>The first time I run the file, nothing happens. I get the console message:</p>
  
  <pre><code>&gt;&gt;&gt; runfile('C:/my_filepath/thing.py')
  </code></pre>
  
  <p>and nothing more. If I run the file again, then it executes, prints the stuff, the plot comes up etc:</p>
  
  <pre><code>runfile('C:/my_filepath/thing.py')
  ~the stuff it's supposed to print~
  </code></pre>
  
  <p>More info: This problem only occurs on my laptop computer, which leads me to believe it has something to do with my matplotlib installation (mysterious to me because I installed an identical Anaconda package on my desktop and there is no issue). On the laptop the plot window is separate, and on the desktop the plot displays in the console. Maybe that's relevant. </p>
  
  <p>Has anyone had this issue? </p>
  
  <p>edit2: This only happens if I try to run the program multiple times in the same console. If I run the script in a fresh console it works fine, then you have to run it twice for every execution. If I close the matplotlib window, kill the console, and open a new one, I can execute it fine every time. </p>
  
  <p>edit: here is a working example which exhibits the odd behavior. don't make fun of my code - I learned python over a weekend</p>
  
  <pre><code>import re
  import numpy as np
  from numpy import genfromtxt as gft
  import matplotlib.pyplot as plt
  
  def getnames(Pattern):
      #construct the list of files in the directory
      CSVList = []
      for FileName in os.listdir():
          if re.compile(Pattern).search(FileName):
              CSVList.append(FileName)
  
      print(len(CSVList), 'files found.')
  
  
      #sort the list by the integer values of the last 4 digits of the    filename
      CSVList.sort(key=lambda x: int(x.rsplit('.')[0][-4:]))
  
  
      return CSVList
  
  def xy_extract(Data):
      x = np.array([row[0] for row in Data])
      y = np.array([row[1] for row in Data])
  
      return x, y
  
  CSVList = getnames('(?i)\.csv$')
  print(CSVList)
  
  #plt.figure(figsize=(10,5))
  plt.xlim(799,3999)
  
  for Filename in CSVList:
      Data = gft(Filename, delimiter=',',skip_header=2)
      x, y = xy_extract(Data)
  
      plt.plot(x, y,label=Filename)
  
  plt.show()
  </code></pre>