Spider Run button doesn't behave as expected - python-2.7

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.

Related

In pycharm .py file is run in python console mode

When I ran my program in pycharm that time my .py file output came in python console mode and also did not see the "Run" option in below(before debug).Please help me to set "Run" option.So I can execute my program using "Run" option.Attaching screen shot.
current is :
Expected:

PyCharm - colored output in django console

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.

Run a executable by python at background and still running

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.

Application called with Subprocess from PySide GUI application (py2exe) stopped working

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)

Run python script with droneapi without terminal

I managed to run examples in command prompt after running mavproxy.py and loading droneapi. But when I double click on on my script, it throws me "'local_connect' is not defined", it runs in terminal as was told above, but I cannot run it only with double click. So my question is: Is there any way to run script using droneapi only with double click?
using Windows 8.1
Thanks in advance
You'll want to look at the Running an App/Example section of the guide. For now, you can only run a DroneKit script by launching it from inside the MAVProxy terminal. For example, after launching:
$ mavproxy.py --master=127.0.0.1:14550
MANUAL> module load droneapi.module.api
DroneAPI loaded
You can use the api start command to run a local script:
MANUAL> api start vehicle_state.py
STABILIZE>
Get all vehicle attribute values:
Location: Attitude: Attitude:pitch=-0.00405988190323,yaw=-0.0973932668567,roll=-0.00393210304901
Velocity: [0.06, -0.07, 0.0]
GPS: GPSInfo:fix=3,num_sat=10
groundspeed: 0.0
airspeed: 0.0
mount_status: [None, None, None]
Mode: STABILIZE
Armed: False
I think Sony Nguyen is asking for running the vehicle_state.py outside the Mavproxy command prompt, just like runnning the .py file normally.
I'm also looking for a solution as well.
You can only run dronekit from mavproxy at the moment (its structured as a mavproxy module, there are plans to restructure it), however if you simply want to avoid having to load up MavProxy and then running code manually, you can use the cmd flag:
mavproxy.py --cmd="api start app.py"