Curses giving error when running curses.initscr() - python - python-3.x

Background:
I recently installed curses with pip install curses
I found a couple tutorials online (https://www.devdungeon.com/content/curses-programming-python, https://docs.python.org/3/howto/curses.html, https://www.youtube.com/watch?v=rbasThWVb-c)
I always constantly testrun my code when I install a new package or learn something new
Whenever I run s = curses.initscr() in IDLE, I get this error message:
File "C:/Users/jacob/AppData/Local/Programs/Python/Python37-32/screentest.py", line 3, in <module>
s = curses.initscr()
File "C:\Users\jacob\AppData\Local\Programs\Python\Python37-32\lib\curses\__init__.py", line 30, in initscr
fd=_sys.__stdout__.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'
This is the message from PyCharm:
Redirection is not supported.
Process finished with exit code 1
And when I run a sample snippet from DevDungeon,
print("Preparing to initialize screen...")
screen = curses.initscr()
print("Screen initialized.")
screen.refresh()
curses.napms(2000)
curses.endwin()
print("Window ended.")
in command prompt with python booted, it just gives an uninteractable blank screen.
Is the thing happening in shell correct?
What the hell is going on?
How can I fix this?
please help thank you

sys.__stdout__ is the original sys.stdout for a python process. When you start python with pythonw.exe, which only exists on Windows, python initially executes stdout = __stdout__ = None. (I am not sure about what happens on *nix.) pythonw.exe is used to run python with a GUI UI without an associated text console. On Windows, the IDLE icons and Start menu entries run python with pythonw. Same with other GUI IDEs.
curses runs with text terminals or consoles. It assumes that sys.__stdout__ is such. It cannot work when sys.__stdout__ is None.
If you start IDLE from a command line terminal/console with python -m idlelib, sys.__stdout__ will be that terminal, and it will have a fileno(). What will happen after that I do not know. If your program uses curses, you are likely better off to run it in the text console.

Related

Error launching Python IDLE via Crostini Terminal tkinter.TclError

I recently got a Lenovo Duet Chromebook and had started using the Linux terminal to install Python and was using the IDLE Shell to run some basic commands. This was working fine until today, and now IDLE is failing to launch.
If I launch IDLE from the app drawer, then it pops up, but then instantly closes.
Running from the Terminal, I get the following error:
X Error of failed request: BadDrawable (invalid Pixmap or Window parameter)
Major opcode of failed request: 55 (X_CreateGC)
Resource id in failed request: 0x40004a
Serial number of failed request: 1142
Current serial number in output stream: 1152
If I try to launch again, I get a slightly different error:
Traceback (most recent call last):
File "/usr/bin/idle", line 5, in <module> main()
File "/usr/lib/python3.9/idlelib/pyshell.py", line 1483, in main root = Tk(className="Idle")
File "/usr/lib/python3.9/tkinter/init.py", line 2270, in init self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0"
I've tried turning off Linux and back on, and installing the packages again. I tried a powerwash as well, but after installing everything I still get the same error.
The first error is coming from a call in the X11 client library, XCreateGC(), or rather it is coming from the Xserver as it is processing the message. That's how such calls error out, and it is really rare! (I'd love to know what caused it; it's really not supposed to happen. If it is difficult to hunt down what caused it the problem in the first place, fixing it is even more difficult.)
The subsequent messages are about failing to connect to the Xserver at all, almost as if it has crashed. Which is very odd! If the Xserver has crashed, it needs to be restarted before you can connect to it again. That in turn might require some effort to rebuild session credentials. (It's possible that the easiest thing to do is to just reboot.)
Tk (and hence, by extension, tkinter) doesn't talk to other ways of rendering to the screen on ordinary Unix, mainly because nobody has ever contributed an alternate mechanism. Such alternates exist for some Unix derivatives such as macOS and Android, but to my knowledge nobody's done Wayland yet. I could be wrong.
I have almost the same error.
Python 3.9.2 installed (regular deb packages) on Linux environment (Crostini?) running on Asus Chromebook with up to date system.
I'm able to run 'regular' X11 applications like gimp (xeyes and xlogo work as well), but there are issues with python's tkinter.
Trivial program like:
import tkinter
tkinter.Tk()
displays empty window as expected, but more complex tkinter apps fail.
Running IDLE produces:
wintermute#penguin:~$ idle
Traceback (most recent call last):
File "/usr/bin/idle", line 5, in <module>
main()
File "/usr/lib/python3.9/idlelib/pyshell.py", line 1483, in main
root = Tk(className="Idle")
File "/usr/lib/python3.9/tkinter/__init__.py", line 2270, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0"
wintermute#penguin:~$
Issue has been resolved by a ChromeOS update, details here: https://bugs.chromium.org/p/chromium/issues/detail?id=1314921

Run python script with module/ modules already imported

I have tried nearly all solutions in my mind to do this, but the fact is I can't run a python script with modules imported already.
Here's my code for the module cls.py:
import os
def cls():
os.system('cls')
Given below is the code for opening python in cmd:
#echo off
python
pause
What I need is to open the python in the command prompt with the module cls imported. Also, when I try python -m <module> way, it doesn't show any errors, but the program ends.
Any help would be greatly appreciated and thanks in advance.
Saw this question related to mine, but it is not my problem: Run python script that imports modules with batch file
I think what you'r looking for is the interactive mode:
-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command
So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.
Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.

python script fails in terminal via 'import psutil'

I wrote a simple script to check the battery time on my laptop (the built in one on xubuntu seems somewhat inaccurate), the code runs fine in a python terminal but when I run it without starting in the python terminal I get an error related to 'import psutil'.
the script is
I'm very inexperienced with python and haven't been able to find anything helpful online.
#!/usr/bin/env python3.6.7
import psutil
import time
a=psutil.sensors_battery()
b=str(a)
c=b[b.rfind(start)+len(start):b.rfind(end)]
d=int(c)/3600
print(d)
time.sleep(130)
exit()
what I expected is the value c divided by 3600 to be printed on the terminal, display for a while and then exit. instead i get
Desktop$ python battery.py
Traceback (most recent call last):
File "battery.py", line 2, in <module>
import psutil
ImportError: No module named psutil
i also tried running through the menu options when i right click the script and select python3.6, where a terminal flashes and closes immediatly (to quickly to read any of the print in it).
i've also changed
#!/usr/bin/env python3
#!/usr/bin/env python3.6
#!/usr/bin/env python3.6.7
each has the same result
thank you for any advice
david

Python3 interactive mode on Linux launches the codes twice

I have written a chess program in Python 3.4.3 and I am running the Python 3 interpreter in the interactive mode as follows:
python3 -i chess.py
However, the code after the class definitions get invoked twice and I do not know why. My code is on pastebin
You should remove the line from chess import * that is at the end of the file, it should not be needed.
Also, it is common to make sure that some of the code is not executed unless the code in the module is executed as a script.
if __name__ == '__main__':
# Not executed if the module is imported
g = Game()

Finding IDLE in Python 3

I am running python 3.3.5. I can't figure out how to launch the IDLE IDE. I installed everything correctly but the IDLE doesn't appear anywhere.
To start it from the command line in Python 3.3+:
$ python3 -midlelib
Or in the code:
import idlelib.PyShell
idlelib.PyShell.main()
You will find an idle.py file at C:\Python33\Lib\idlelib.
There are 2 idle.pys file and both of them work. The first opens a command prompt alongside idle and the other doesn't.

Resources