4

Using Sublime Text 2 or Atom (maybe other editors too), when running a simple plotting script in Python, the Python Launcher icon (the rocket) pops up every time I run the code. The icons add up in the Dock , and I have to close them individually. Is there any configuration to avoid this behavior?

With Sublime Text, for instance, Command + B is the default shortcut I use to run a script. I have not found this issue addressed in Atom's or Sublime Text's documentation (neither in Stack Exchange).

The script that I use is a basic fit to some data:

import csv
import matplotlib.pyplot as plt
from scipy.interpolate import *

with open('data.csv', 'rU') as mycsv:
    data = csv.reader(mycsv)
    x = []
    y = []
    for row in data:
        x.append(float(row[0]))
        y.append(float(row[1]))

p = polyfit(x, y, 2)
xp = linspace(-5, 1, 1000)

plt.scatter(x, y)
plt.plot(xp,polyval(p,xp),'r-', label='p1')
plt.show()
  • Welcome to Ask Different :) Can you please edit the post and describe how you are running the Python apps using text editors? – Nimesh Neema May 27 at 12:29
  • @NimeshNeema Sure, check the edit. I have the feeling that the solution may not be Python-related, but macOS-related. – cwasdwa May 27 at 13:23
  • WHat is the script's extension? and how did you install python? – Mark May 27 at 14:54
  • If I remember correctly, I installed Python via HomeBrew (that was a while ago!). The script is not very extent: some lines that read a .csv, and some other that plot the data. The script runs good and fast. I have not found in Python language a line that closes the plot after showing it that works for me. – cwasdwa May 27 at 15:15
  • Looks like your python isn't exiting properly. Do you have an exit() line? – benwiggy May 27 at 15:24
1

I'm not familiar with pyplot, but from the documentation, it looks like the show() method 'holds onto' ("blocks") the script until it is closed by some user interaction.

In non-interactive mode, display all figures and block until the figures have been closed;
A single experimental keyword argument, block, may be set to True or False to override the blocking behavior described above.

Other methods like ginput() and waitforbuttonpress() describe the blocking process.

You could test this by commenting out the last line and see if the icons persist.

  • This may well be the case, however I cannot get rid of the duplicated plots by commenting plt.show()or using plt.close(), even using plt.figure() prior to it. By 'closed by some user interaction' I would expect also re-running the script. – cwasdwa May 31 at 10:31
  • No: running the script again does not close previous executions. A script is the instructions for a process: The process can be run following the same instructions over and over again, even if one process has not yet finished. – benwiggy May 31 at 11:04
1

In your setup Sublime Text is running the python script by running python script.py as if from the command line. This starts a new python interpreter process for each run. This is documented (and is the actual example used) in the Build Systems part Sublime Text Manual

Normally this is OK but in your case you are running a script that will stay open until it is manually closes. All these processes are independant and have no idea of the others and so you just keep getting more.

To have only one python process and thus one icon on the desktop you need a different set up. In general this is using a REPL (read-eval-print-loop) which passes all your python commands to one process. I am not a sublime text user so I don't know if a REPL from it can show graphics.

In your case of lots of graphs I would look at Juypter Notebooks. In Juypter python is started as an external process the kernal and your editor sends python command to it, thus there is one Python process that knows about all the graphs. A quick google suggests that there is a package Hermes for Sublime Text.

  • I do have the packages SublimeREPL and Hermes installed. I am in fact running the script via python3 using SublimeREPL. The build system I use is (maybe it does not sound familiar for non-SublimeText users): { "cmd": ["python3", "-i", "-u", "$file"], "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)", "selector": "source.python" }. This is, I am actually using the REPL package. – cwasdwa May 31 at 14:25
  • 1
    No you are using the build that starts a new python process. See the command (cmd) you are actually using. Read the Hermes document packagecontrol.io/packages/Hermes on how to execute code in the REPL – Mark May 31 at 14:29
  • Additionally, I have used Jupyter, but what I want by setting up a Python environment in SublimeText is to be able to run a code that can be long side-by-side with my plot. I also know of the existence of environments like Jupyter Lab or Spyder, but I don't actually need that much. Just a clean text editor and a figure (or figures). In other words, I want a clean environment, without add-ons that I don't use... – cwasdwa May 31 at 14:32
  • I just configured Hermes, connecting first a kernel (obtained in Jupyter via %connect_info), and then executing the block. Now I obtain the output directly on the screen, without Python launcher icons popping up. I still fail, however, at having both windows side by side, as in the documentation. Instead, a new tab opens, hiding the main code. Anyway, this is a smaller issue that I can eventually figure out. Gracias. – cwasdwa May 31 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .