The architecture of the program consists of a PySide GUI executable build with Py2exe. Furthermore it has a second python console application build with Py2exe.
The GUI-App starts the console application using a named Pipe to be able to send CTRL_C and capture stdout/stderr. This is done using the pywin32 module.
Inside the console application several other Windows applications are executed in blocking mode using the subprocess module. Now the console application got extended by some code to execute another program in nonblocking mode.
If the GUI application and the Console application are executed (Before being build to a Py2exe App) everything works as expected and wanted.
If the applications are build with Py2exe the external Windows application freezes and a windows error pops up. (application stopped working)
If the execution code of the external Windows app is isolated and a py2exe executable build, everything works.
So the problem must be py2exe and calling the console app from a GUI app.
I hope someone can point me in the right direction.
I got asked for a minimal example. The problem is that the architecture does not really allow a minimal working example. I tried my best and i hope the given Example draws a clearer image of the problem. As explained
through testing the problem could be reduced to py2exe. As soon as the GUI app is "compiled" to start of a nonblocking application inside the console app works but the new application causes a windows error.
Minimal Example:
Console App console.py:
import logging
import subprocess
from time import sleep
def exec_program_nonblocking(args, cwd=None):
if cwd:
logging.debug("Change working directory to: %s", cwd)
subprocess.Popen(args,
cwd=cwd,
universal_newlines=True)
def main():
args = ["program.exe", "arg1", "arg2"]
exec_program_nonblocking(args)
while (True):
print "do something \n"
sleep(5)
if __name__ == '__main__':
main()
setup.py for console App:
from distutils.core import setup
import py2exe
setup(console=['console.py'])
PySide GUI application gui_app.py:
from PySide.QtGui import QApplication
def start_console_app():
pass
# complex implementation of openening named pipe,
# handling callbacks, allowing to send CTRL_C to new process etc.
# Minimal example is not possible...
app = QtGui.QApplication([])
button = QtGui.QPushButton('Start Console App')
button.clicked.connect(start_console_app)
button.show()
app.exec_()
setup.py for GUI Application:
from distutils.core import setup
import py2exe
setup(
options = {
'py2exe' : {
'packages' : 'encodings',
'unbuffered' : True
#dll_excludes....
}
},
zipfile = None,
windows = ['gui_app.py']
)
For me a solution posted here worked for me. Somehow py2exe messed with the handles passed from the father process, the GUI app, to the child-child process. It does not even happen with all executables that are subprocesses from the console application.
The subprocess module offers a parameter close_fds to prevent the childprocess from inherating the handles from the father process. This was not working for my application. Still, it must be related to py2exe.
giving the DETACHED_PROCESS flag to the parameter creationflags a new console is opened and the process successfully started!
The only drawback is, that stdout/stderr cannot be captured, as far as i know.
working example:
import subprocess
DETACHED_PROCESS = 0x00000008
subprocess.Popen(args,
cwd=cwd,
creationflags=DETACHED_PROCESS)
Related
I'm able to execute an exe file via command prompt using below line:
process = subprocess.Popen("LogCollector.EXE ")
But the LogCollector.EXE GUI is still visible, Please suggest some method to run this completely in hidden way.
import os
os.system('start /MIN notepad.exe');
Use this command in the window, it will run the application and minimize the application.
Maybe it will help you
I am a newbie with python and today I have tried to set the Spyder IDE to run PySide clases in order to make some applications. The thing is, once Spyder was installed (Spyder (Python 2.7) in Ubuntu) I tried to launch a hello world in order to check if all was correctly tied up. Before installing Spyder I had Python 2.7 with the PySide packages properly intalled. This is what my PySide Hello World program look like:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()
When I tried to run the code pushing the Run File button (F5) the console only shows
runfile('/home/leo/.spyder2/temp.py', wdir='/home/leo/.spyder2')
And the same thing selectig a IPython console throws
runfile('/home/leo/.spyder2/temp.py', wdir='/home/leo/.spyder2')
It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console.
It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console.
It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console.
...etc...
But nothing happens, no application launched. However, if I select the whole text in the code editor, and then press F9 (which only executes the selected lines) everything works fine and the test application pops up!!! This happens with both Console and IPython Console.
I looked for some explanation about this awkward behaviour on the internet (like this post spider IDE python. Difference in running by pressing F5 and F9?) but in the end my question ends unanswered...
One more thing, executing the code in IPython console succesfully launches the test application, but once it is closed, a second attempt in the same console throws en error
RuntimeError: A QApplication instance already exists.
Any help would be very appreciated, thank you community.
The problem is that when I'm lauching dev-server through Kubuntu's Konsole app, debug output is colored:
But wher I'm launching it trough built-in "Run" or "Debug" in PyCharm, the output in PyCharm's console is all red:
Is there a way to make output in PyCharm colored? Because I can't find anything related in Settings.
UPD: link to issue on PyCharm issue tracker
As I see some people woting on this question, so here is the link to this issue on PyCharm issue tracker - youtrack.jetbrains.com/issue/PY-19790. If you will vote for this feature, it will be released faster!
Some time ago I wrote a code snippet that does the thing for me. It's not a perfect solution, but does the trick. BSD License
To use it:
Option 1: git clone/download this and import it somewhere (e.g. django development settings). Read the module docstring and the readme for details.
Option 2: include the code snippet below into your django development/debug settings (or anywhere else, where it will be run early). It's automatically verified with SHA-2 against file changes.
try: # Colored logger CaaS. Auto downloaded and verified.
import os
import hashlib
from urllib import request
url, sha256 = "https://lab.it.hs-hannover.de/lukyanch/pydevutils/raw/c531eaf7/colored_logger.py", "083e1a39cfdbe17a7126188b5477fb8f324be8106a39ed4a00faeb3f18c5aedc"
cached_file = "/tmp/{0}.py".format(sha256)
code = bool(os.path.exists(cached_file) or [request.urlretrieve(url, cached_file), print("Downloaded: " + url)]) and open(cached_file, "r").read()
assert hashlib.sha256(code.encode()).hexdigest() == sha256, os.remove(cached_file) or "Bad content: " + cached_file
exec(code)
except Exception as e:
print("No colored logger: {e.__class__.__name__}: {e}".format(e=e))
"grep console" is the plugin which handles this perfectly:
https://plugins.jetbrains.com/plugin/7125-grep-console
This is because the development console for PyCharm is not using the same kind of standards to display colors as your normal shell terminal (they are two very different things).
The output console for PyCharm is just a display of commands (its not a full shell with all the bells and whistles of your local terminal emulator).
You can embed a terminal into PyCharm (see the documentation on how to set it up) but doing so will not allow you to redirect the output of the built-in run commands to the terminal window. It will enable you to run arbitrary shell commands there, and avoid having to switch over to a terminal console.
Keep in mind if you have any special shell customization or fonts/glyphs these may not appear correctly in the embedded terminal, because it is using a different set of fonts and configuration.
You can try and raise this as an enhancement at the PyCharm bug tracker; if it gets enough votes it may make it in to the next release.
I'm using the pyvbox, the complete implementation of the virtualBox main API. In my use case i just want to write a python code in order to start and stop VMs. When i use python GUI every things works fine.But when i try to run the python code from the CMD i got this error:
echec de l'ouverture de session pour la machine 'test'
Microsoft Visual C++ Runtime Library
Runtime Error!
Program: C:\Program Files\Oracle\VirtualBox\VBoxSVC.exe
R6025
-pure virtual function call
PS:
python version 2.7.11
pyvbox 1.0.0
OS windows 8
startvm.py:
import virtualbox
vbox = virtualbox.VirtualBox()
session = virtualbox.Session()
vm = vbox.find_machine('test_vm')
progress = vm.launch_vm_process(session, 'gui', '')
probably you don't need any help on this anymore :D, but I write it here anyway, maybe it can be helpful to someone.
Starting and stopping a VM with the python pyvbox library is very simple:
import virtualbox
from vboxapi import VirtualBoxManager
vbox = virtualbox.VirtualBox()
session = virtualbox.Session()
# This part starts the machine:
# machine_name is a simple string containing the name of your VM. For example "ubuntu"
machine = vbox.find_machine(machine_name)
proc = machine.launch_vm_process(session, "headless")
proc.wait_for_completion(timeout=-1)
# This part stops it:
process, unused_variable = session.machine.take_snapshot("snapshot_name", "snapshot_description", False)
# False means "do not pause the machine while snapshotting"
process.wait_for_completion(timeout=-1)
session.unlock_machine()
I created a simple script to automatically snapshot my virtual machines.
GitHub:
https://github.com/Meru3m/virtualbox-snapshotter
I have a executable made by py2exe which verifies whether my VPN is connected or not at infinite loop running on Windows. I want to make sure it runs in background or hidden, I searched for several forums and I found 2 scripts worked partially.
Rename script to scrypt.py and run py2exe again, the .exe hide when I run it, but close or vanish. Doesn't continue running after the click.
I am made another exe to call the first
import os
import subprocess
os.chdir("C:\Users\dl\Documents\Log\Py")
proc = subprocess.Popen('ipLog.exe', creationflags=subprocess.SW_HIDE, shell=True)
proc.wait()
or
os.chdir("C:\Users\dl\Documents\Log\Py")
proc = subprocess.Popen('ipLog.exe', creationflags=subprocess.SW_HIDE, shell=True)
Works but the first command still visible, and when i close it, the first exe call by it quit too.
I tried install a module call self.hide but i cant.
I am newbie in python and try to change my hobbies vb, vba to python.
Thanks for any help.
I found a solution in this thread How to start daemon process from python on windows?. Thank you all people helped with this thread, help my script too.