I am writing an IRC bot in Python.
I wish to make stand-alone binaries for Linux and Windows of it. And mainly I wish that when the bot initiates, the console window should hide and the user should not be able to see the window.
What can I do for that?
This will hide your console. Implement this lines in your code first to start hiding your console at first.
Simply save it with a
.pyw
extension. This will prevent the console window from opening.Explanation at the bottom of section 2.2.2
In linux, just run it, no problem. In Windows, you want to use the pythonw executable.
Update
Okay, if I understand the question in the comments, you're asking how to make the command window in which you've started the bot from the command line go away afterwards?
I think that's right. In any case, now you can close the terminal.
On Unix Systems (including Linux, macOS, and BSD)
Use
nohup mypythonprog &
, and you can close the terminal window without disrupting the process. You can also runexit
if you are running in the cloud and don't want to leave a hanging shell process.On Windows Systems
Save the program with a
.pyw
extension and now it will open withpythonw.exe
. No shell window.For example, if you have
foo.py
, you need to rename it tofoo.pyw
.If all you want to do is run your Python Script on a windows computer that has the Python Interpreter installed, converting the extension of your saved script from '.py' to '.pyw' should do the trick.
But if you're using py2exe to convert your script into a standalone application that would run on any windows machine, you will need to make the following changes to your 'setup.py' file.
The following example is of a simple python-GUI made using Tkinter:
Change "console" in the code above to "windows"..
This will only open the Tkinter generated GUI and no console window.
Some additional info. for situations that'll need the win32gui solution posted by Mohsen Haddadi earlier in this thread:
As of python 361, win32gui & win32con are not part of the python std library. To use them, pywin32 package will need to be installed; now possible via pip.
More background info on pywin32 package is at: How to use the win32gui module with Python?.
Also, to apply discretion while closing a window so as to not inadvertently close any window in the foreground, the resolution could be extended along the lines of the following: