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
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.
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
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()
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.