What is this Python Icon? - python

Sometimes when I run python scripts in terminal a python instance opens up
I've seen matplotlib and ghost cause this. The only way to close it seems to be to close the python terminal.
What exactly is this instance and how do I have it close automatically after my script executes?

On OS X the Dock will display an icon for Python whenever you do something that interacts with the GUI. The most common reason for this is using matplotlib with a GUI backend (e.g. PyQt). When using an interactive backend (e.g. PyQt) you will normally see a rocket icon after you call plt.show() which will remain on the Dock until after the window is closed and process that interacted with the GUI exits.
When you are working interactively in the Python shell, the icon stays there not because the Terminal is still open (or script is still executing) but because the Python process is still active. This process is probably maintaining a GUI event loop in the background.
There are a few solutions:
In matplotlib you can use plt.show(block=False) to detach the event loop, meaning the icon will disappear once the window is closed.
Set a non-interactive backend, e.g. matplotlib.use("Agg") to avoid creating the windows at all. Of course, you can't see anything.
Modify the Python.app .plist to suppress icons.
For the latter, the instructions here (Dr. Drang) are as follows. Note this is for the system Python, for other locations you'll need to find the .plist elsewhere:
cd /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/
# NB the following file is binary, use a compatible editor or convert with
sudo plutil -convert xml1 Info.plist # Convert to XML
sudo chmod 666 Info.plist # Make editable
Then add the following before the final </dict>
<key>LSUIElement</key>
<true/>
Return to binary and fix permissions:
sudo chmod 644 Info.plist
sudo plutil -convert binary1 Info.plist

PyCharm doesn't consider the script to be complete until any popups it spawns are closed. This is evident when you run matplotlib and a plot pops up. If PyCharm closed the window upon completion of the script, the window would immediately close before you could even view your chart.
There are two ways to finish the script. Either close the popups, or click the stop button in PyCharm.

Related

Pinning python application running through Python interpreter

I have an application I run using pythonw.exe. I just click on my .pyw file and it runs. It's using Qt and I have set up an icon that both shows in the window and in the taskbar.
However when I try to pin it, it pins the python IDLE incorrectly instead of the application itself. It makes some sense since it is a python file run from pythonw.exe. Example below:
So how do I proceed to run the application the right way so I can pin it to the taskbar?
As eryksun said you need a shortcut with pythonw.exe "Path to pyw file" in target.

Python- Alternative terminal in Atom text editor

Right now I'm running Python in Atom with Platformio as my terminal. First off, what's the keyboard shortcut to run the file? Secondly, is there a better terminal package out there? I'd like more features such as right clicking and selecting run the file or something that makes running the file easier (maybe a run button). I switch between files quite frequently so I'd like an alternative other than using the up arrow to run previous command.
I use Atom for python. The best terminal plugin I have found is Platformio. I have a plugin named script that allows me to run python scripts from the editor window. The command also has a shortcut to speed up the process. Load script and it will appear under the packages menu. It also defines the shortcut for 'run script' as the command-I keys.
Lately I've been running python script using the Hydrogen plugin. This allows in-line plotting along with running your script. Answers and plots appear in the editor pane. Its very nice if you have to plot.

How to open a window from the command line within a Python script?

I am working on a python script to analyze astronomy images and I am trying to open a DS9 window within a python script (DS9 is a utility that allows images to be interactively viewed and analyzed). Usually I would open DS9 by going into the Linux terminal and typing:
>ds9 &
and then it would pop up in another window.
I tried to mimic this in my python script by writing the following line:
os.system('ds9 &')
When I would run the script the DS9 window would pop up but the rest of the script would not run until I closed the DS9 window. This gave me errors because the tasks that followed needed a DS9 window to be opened.
I am wondering if there is a way to open a window from within a python scripts and still have the rest of the script continue running.
Perhaps:
os.system('ds9 &')
isn't the right approach?
You can use subprocess module.
subprocess is a newer way to spawn processes rather than using os.spawn*() or os.system().
In your case:
import subprocess
subprocess.Popen(["ds9"])
This should run ds9 in background.
See the documentation here.

Is there a way to prevent a python window from being seen when opened?

So I want to make a Python file that runs all the code within, but the window is invisible. So the user won't see the window in the task bar, or really anywhere on his screen.
How would I do something like that?
To make a script not open the terminal window change the extension of your script to .pyw which will cause the script to be executed by pythonw.exe by default. This suppresses the terminal window on startup.
If you would like all scripts to open like this you should read up on Executing Scripts.
Maybe you'd want to use start pythonw.exe

Hide console for Tkinter app on OSX

I'm trying to hide the Terminal when I launch a GUI Tkinter based app, but when I double click the app.py file on OSX, the Terminal window appears. I've tried changing the extension to .pyw and tried launching it with /usr/bin/pythonw, but no matter what, the Terminal window still appears.
I've even tried adding the try/except below, but when I run it I get the error: 'invalid command name "console"' in the Terminal window that appears.
from Tkinter import *
class MainWindow(Tk):
def __init__(self):
Tk.__init__(self)
try:
self.tk.call('console', 'hide')
except TclError, err:
print err
win = MainWindow()
win.mainloop()
I haven't been able to find any way to hide the Terminal window from appearing. Anybody got any ideas?
By double-clicking a .py file on OS X, you are likely launching a Python gui instance via the Python Launcher.app supplied with OS X Pythons. You can verify that by selecting the .py file in the Finder and doing a Get Info on it. Python Launcher is a very simple-minded app that starts Python via a Terminal.app command. To directly launch your own Python GUI app, the preferred approach is to create a simple app using py2app. There's a brief tutorial here.
EDIT:
There are other ways, of course, but most likely any of them would be adding more levels of indirection. To make a normal launchable, "double-clickable" application, you need some sort of app structure. That's what py2app lets you create directly.
A very simple-minded alternative is to take advantage of the AppleScript Editor's ability to create a launcher app. In the AppleScript editor:
/Applications/Utilities/AppleScript
Editor.app in OS X 10.6
/Applications/AppleScript/Script
Editor.app in 10.5
make a new script similar to this:
do shell script "/path/to/python /path/to/script.py &> /dev/null &"
and then Save As.. with File Format -> Application. Then you'll have a double-clickable app that will launch another app. You can create something similar with Apple's Automater.app. But, under the covers, they are doing something similar to what py2app does for you, just with more layers on top.
Adding to the answer by Ned Deily, im my case when I tried to launch the Python application using an AppleScript application, it did not work initially. I discovered that it has something to to with some kind of encoding error (I am using UTF-8 and in the past I had felt need to configure it to UTF-8).
So, after further investigation, I discovered that I can accomplish this by creating an AppleScript application with the following code (adjusting the paths of python3 and of the Python application as needed):
do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; /usr/local/bin/python3 '/Users/USER/FOLDER/SCRIPT.py' &> /dev/null &"
It launches the Python application without any Terminal windows. The AppleScript application can then be personalised with a custom icon as usual, and can be placed in the Dock. When clicked, it will launch the Python intepreter, that still shows up in Dock, but with no visible windows.
I think this may be useful to other users.
'console hide' doesn't hide the Terminal in OS X. It hides Tk's built-in console, which is really a relic from the MacOS Classic days (and which is still commonly used on Windows).

Categories