Why is spyder console responding inappropriately? - python

I have installed spyder v3.0.2 as part of Anaconda (in Windows 7), and have started working through the tutorial.
However, the python console does not seem to be responding correctly to input, either directly in the console or from a script via runfile.
There seem to be two things going on:
(1) The prompt appears too quickly so that it cuts into output
(2) Each time my script (which defines a hello function including one call to print, and calls it) is run, I do not see the expected output until I press Enter. From then on the console seems to need me to press Enter another time to produce the normal response you would expect from Enter. This leads to unexpected behaviour, particularly around the regular (>>>) and continuation (...) prompts. It increments each time the script is re-run, so that after running three times you need to press Enter an additional three times. For example:
Am I missing something obvious? My guess would be that it has something to do with line ending encodings but I can't figure out how to stop it. Also to my mind that still wouldn't explain issue (1).
Note that the IPython console seems to work without these issues (which of course I can use but I don't like the fact that there is clearly something amiss more generally...). Also, I tried installing the Mac version on my MacBook and don't have the same problem.

Related

Is it possible to make pycharm's run display section display the same as actually running the program would?

Disclaimer: Sorry if the question is stupid or repeated, I've tried to find similar ones that answer what I need to know but I couldn't. I've started to learn programming about 1 month ago and this is my second time on this website. Feel free to point out any errors or better ways to formulate my questions on stackoverflow, I'll be grateful.
Context:
I was trying to find out a way to print a string - in this case ' º ' - after my user's input - an angle -, on the same line.
I need an alternative way to do this, or help with the one I'm using.
What I got from my research is that, apparently, using the command os.system(cls) should erase the previous line, and putting \033[A before the string should move the cursor up one line. So using these two together should erase the previous line and then overwrite it.
Here's my try:
from os import system
cls = lambda: system('cls')
angle = float(input(f'Insert an angle:'))
cls()
print(f'\033[AInsert an angle:{angle}º')
Desired result on run:
Insert an angle: *60*º # being 60 the user's input
Actual result on pycharm:
Insert an angle:60
Insert an angle:60.0º # for some reason, you can't see it when paste it here, but there's a symbol of a crossed rectangle on the beginning of this line on Pycharm's run
How it looks on pycharm's run terminal
As you can see, the line isn't getting overwritten, only repeated.
What is weird is that when I run this program with Python 3.8 instead of Pycharm, it works as intended, but, on Pycharm, the line isn't overwritten. Instead, Pycharm just prints a crossed rectangle symbol.
Why does it work when executing the file with Python 3.8, but not when pressing "run" on pycharm?
Is there a way to avoid it?
Are there better alternatives to printing a string on the same line as an input?
In cases where I need special printing (ANSII escape codes, backspacing...), I use the actual Terminal, not the Python Console.
For whatever reason, interactive consoles, regardless of IDE, seem to have issues with handing specialties like that. With the normal Terminal, it works as expected:
I have never found a way of having the interactive console handle cases like this.

TIS/TSM non-main thread error; pygame script triggered by hotkey (rumps, pygame, keyboard)

I'm writing a python app, whose main purpose is to run a minigame (using the 'pygame' library), whenever I use a hotkey (which currently uses the 'keyboard' library). I want this hotkey to be recognized universally, so I'm packaging the whole thing as a status bar app (using the 'rumps' library).
So far, I can start the whole thing, select Play from the status bar dropdown, and it works! Great.
HOWEVER, if I attempt to use the bound hotkey, to run the same function that Play triggers, I get:
python[58226:599749] pid(58226)/euid(0) is calling TIS/TSM in non-main thread
environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
This shows up four times, and is followed up
python[58226:599749] WARNING: nextEventMatchingMask should only be
called from the Main Thread! This will throw an exception in the future.
To check that it wasn't the hotkey itself, I did a test: If I connect the hotkey to a simpler function, like setting an alert, it works fine. It still complains, and gives me the error:
python[60308:620099] -[NSAlert runModal] may only be invoked from the main thread. Behavior on other threads is undefined. (
0 AppKit 0x00007fff2b7f563f -[NSAlert runModal] + 178
1 _objc.cpython-36m-darwin.so 0x000000010c1358c7 ffi_call_unix64 + 79
2 ??? 0x000070000f3b2e50 0x0 + 123145557847632
)
but it does RUN. However if it connects to the Play function, it breaks (and produces the first error, above).
To summarize:
1) Dropdown => run minigame: Success!
2) Hotkey => run minigame: Incomprehensible errors!
I've googled this error, but have only seen explanations that are way over my head; is there a way that I could get around this error, by using different software, or a different approach -- but without having to leave python, or performing some deep and evil hack to an underlying system?
Could I get the hotkey/game to USE the Main Thread environment, somehow? How?
Could I use a vehicle other than a status bar app to listen for the hotkey? (Update: I tried pynput, and got the same non-main thread error.)
Could I do something inside of pygame to not make it as offensive? (At the moment, it's literally just animating a rolling sine wave.)
Could I get the rumps callback function to work? At the moment, it's just not doing ANYTHING, no matter where or how it's called.
(And unfortunately, this on a Mac, because pyhk3 is for windows only, and
wx.Window (which can have hotkeys) is also for windows only)
FURTHER failure: Can't get it to run with Keyboard Maestro either -- I've never used it before, so may well be using it wrong, but it also seems like a VERY simple command, that just flat-out has no response whatsoever.
Aaand ... the Automator script runs, but -- not with the hotkey! (EDIT: The hotkey was apparently taken. But a different one worked! See below.)
This problem is a MacOS 10.13 Sierra error. It is being reported with Processing.org 3d applications, as well as a Program called Synergy.
I'm accepting my own answer because it is the only one so far that solves the problem I was most interested in, i.e. getting the thing to run with a global shortcut, BUT, since I would ideally like to package and distribute this, I would still be very glad to see answers that solved the problem within python, and would gladly award the bounty to any answer that did that.
Finally, finally, made it work with a Service in Automator, and System Preferences > Keyboard > Shortcuts. Along the way, I had to
Explicitly call the python binary I wanted to use, because the bash shell that Automator uses in executing a text script is apparently different from the one in Terminal
Realize that the System Preferences shortcut wasn't working because of the key-combination I first chose, NOT that it was being blocked, because there were no errors or messages when I called it.
Probably all sorts of other things. Good god.

Killing a program with SIGINT is rendering my shell unusable, why?

I'm working on bash in Ubuntu and I have a python program using some threading code. It works well, but after I kill it pressing CTRL+C, the shell just breaks. No input is shown to me (although it still is interpreted in the background) and the only thing that works is the enter key, albeit in a weird fashion.
thunder#machine:~/server/api$ thunder#machine:~/server/api$
Well actually it doesn't work, it just places a new prompt line right next to the previous one.
I think something is messing up with my shell, so I was wondering is there a way to "reset" it? right now I have to open a new shell session and that is very annoying.
To know what is causing this problem to begin with would be great, but I don't think that's possible with the little information I just provided here. And frankly I have no idea of where in my code I could be messing things up, since like I said before the program does work correctly.
Nelson
You need to reset the terminal by typing
resetEnter
into the bash shell (you might need to do it blind, i.e. without seeing the characters being echoed).
See man 1 reset.

Adobe Brackets and running Ruby/Python programs

so I have been trying out various text editors and IDE's recently to see what I like. I really like the web developer side of Brackets, which is what I think it was designed for, but was also messing around with some of the extensions for building and running Python/Ruby programs.
I have tried this, which honestly doesn't seem to do anything from what I can tell:
https://github.com/vhornets/brackets-builder
And this, which adds a run button to Brackets for running the program:
https://github.com/jadbox/brackets-integrated-development
The latter seems to work alright... until you make a small program that asks for user input then the program never runs, the program disappears, and then you have to restart Brackets to get it back. The code I have used is the following:
puts"Please enter your name: "
n = gets. chomp
Or
n = input("Please enter your name: ")
Any idea what might be causing this? I asked the developer, but haven't heard back. I'd probably use Brackets exclusively if it wasn't for that issue.
Tried Sublime Text 3 for a little while, but didn't like the lack of a Brackets like live preview plugin. However, it looks like it runs Python/Ruby (in addition to Java and C/C++) code without issue.
You might want to try a generic extension for running command line tools, such as one of these:
Command Line Shortcuts
Brackets Build System
Command Runner
Brackets Shell access
Brackets Terminal
The Brackets Builder you mentioned above - looks like you choose Edit > Edit Builder to configure the commands you want, and then you can press Ctrl-Alt-B to run them.
Brackets Builder has worked best for me "right out of the box". However, I run into the same problem as you (when wanting to perform console inputs) in both Brackets AND Sublime, but NOT in SciTE.
A good clean work around inside Brackets, for when you need to test programs with console inputs, is to load the extension "Open project in terminal", which will allow you to open a terminal right from your current project folder and run your python script. This allowed me to run your case from Brackets with very little hassle, and of course, it works great in a real terminal.
To make this approach even easier, I've asked the developers on GitHub, thru a raised issue, if they can add support for more terminal programs in Windows, such as ConEmu, and if they could allow intial command line arguments to be loaded, which would make this type of work that you want to do even more seamless and fast. I'll update my post once I see any improvements or alternatives to all the above points.

Using ipdb with emacs' gud without explicit breakpoints in code

I'm using python.el
If I choose 'debugger' from the menu, and enter 'python -m pdb myfile.py', gud starts, and in a split frame I see the (Pdb) prompt in one, and my python code in the other with a caret on the first line, indicating that it's ready to go. For example 'n' steps to the next line and the caret moves accordingly.
If instead I enter 'python -m ipdb myfile.py', the frame splits, and one split is labeled gud, but there's no ipdb console evident. In other words, this way of starting ipdb doesn't seem to work. Ipdb works just fine if I manually insert a breakpoint into my python code using ipdb.set_trace(), except that it does not use the gud interface. Is this intentional so that ipdb's stack trace will work nicely?
If so, that's fine, but is there a way to start ipdb from emacs without manually adding a set_trace() command?
The basic problem here is that gud is looking for a (Pdb) prompt and ipdb doesn't prompt this way. There are three ways to fix this: fix ipdb to give a (Pdb) prompt, fix gud not to need to look for (Pdb) or (my favorite) use something else either on the gud side or on the ipdb side.
The problem with fixing up gud is that it is rather old and to my mind a bit creaky using global variables and not making use of Emacs Lisp data structures available other than lists and cons cells. A total rewrite of gud is called realgud, it is currently in MELPA and ELPA. And ipdb is supported.
The last option is to use something else, so let me suggest the Python trepan debugger which is already integrated into realgud (but not gud since I consider that a dead end). Although the backtraces it gives are not exactly like ipdb's, it does colorize them and the source code.
And recent versions of trepan3k backtraces will even show, on demand, you where in the line you are. So if you had say two calls of a function, like fib() it would distinguish which of the calls function was the one in progress.

Categories

Resources