Cannot use tkinter in Python 3 on macOS - python

Short version: Python 3 is unable to use tkinter on my iMac.
Details and useful info: I'm on macOS Mojave, Version 10.14.3 (18D109). (I can't upgrade for a while yet because of software that won't run on the newer versions.)
In my scripts I'm using #!/usr/bin/env python3 and when I get to the line import tk as tkinter I get this error:
Traceback (most recent call last):
File "/Users/hal/Documents/Dev/HalPy/TestScript.py", line 5, in <module>
import tkinter
File "/Users/hal/.pyenv/versions/3.7.3/lib/python3.7/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
When I tested it, from the command line, I typed python and got the Python shell and the version info (Python 3.7.3 (default, Apr 11 2020, 02:53:05)). Then I tried import tkinter as tk and I get this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/hal/.pyenv/versions/3.7.3/lib/python3.7/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I see the module when I drop into the Help mode and type modules and tkinter shows up in the list of modules. Just in case, I tried installing it with pip, but it wouldn't install (which is what I expected).
So what do I need to do to get Python 3 to recognize tkinter? (Just in case it matters, Python 2 with Tkinter does work.)
Also, I have looked at this question and this one and this is NOT a duplicate. (First note that neither has an accepted answer.) The first one involves reinstalling, and the focus seems to be on Windows, and this is on macOS, which includes it pre-installed. I'm a bit concerned what reinstalling it would do. The 2nd question is having problems because it's being run within the XCode framework and one element of the issue is the wrong shebang and I'm already using the one the answer suggests.

you can find your answer here:
https://blog.lanzani.nl/2020/install-tkinter-macos/

If you are using a Python from any current python.org Python installer for macOS (3.9.0+, 3.8.0+, or 3.7.2+), no further action is needed to use IDLE or tkinter. A built-in version of Tcl/Tk 8.6 will be used.
If you are using macOS 10.6 or later, the Apple-supplied Tcl/Tk 8.5 has serious bugs that can cause application crashes. If you wish to use IDLE or Tkinter, do not use the Apple-supplied Pythons. Instead, install and use a newer version of Python from python.org or a third-party distributor that supplies or links with a newer version of Tcl/Tk.

Related

Linux terminal and Python are mushed together

When I run some commands, which work, for example neofetch, it gives me a Python error.
username#host:~$ as
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
It does that whenever I type an invalid command. This started happening after I installed Python 3.9. I am running Kubuntu with Python 3.10 installed by default.
Modern Linux distributions tend to use a lot of python scripts for internal use; for this reason python gets installed with a plethora of libraries.
The error you are getting is because you replaced that fairly complete system python with a version of your own choosing without providing all the libraries the original came with.
The error you see is emitted by /usr/lib/command-not-found, a script using python3 as found in the system path.
I strongly recommend that you re-install the original python packages and use e.g. anaconda for your specific python needs.
The main takeaway from this is:
DON'T MESS WITH THE SYSTEM PYTHON.
try to find which python are you using using which python and uninstall the first python interpreter it finds.

ImputError: No module named 'tkinter'

I am new to python and wanted to code a simple application to practice a bit more but for whatever reason it is saying there is no module named 'tkinter'. I've tested to make sure tkinter is installed using python -m Tkinter and it works but tkinter won't work with my project. I am using pop_os! Linux and I am using Visual Studio Code as my IDE
Traceback (most recent call last):
File "first_game.py", line 2, in <module>
from tkinter import *
ImportError: No module named tkinter
Capitalisation matters when it comes to names in Python. In Python 2, the library is called Tkinter. In Python 3, it has been renamed tkinter to be consistent with the rest of the standard library.
You can check which version of Python your command line is running with python --version, or by running the following script:
import sys
print(sys.version)
If python -m Tkinter works, then you need to do from Tkinter import *. Case matters when importing packages.
However, if python -m Tkinter works that means you're using python 2.x which is no longer supported. It would be best to switch to python 3.x, in which case the package is named tkinter rather than Tkinter.

Install win32com on MacOs and Linux

My question is simple: can I install win32com python API (pywin32 notably) on an OS that is not Windows?
I have been trying, on Mac, multiple versions of pip install pywin32, all have failed. An example below:
If you managed to install it on Linux I am also interested as I wrote it.
I am using python 3.7.8.
Thanks.
Win32COM is part of [GitHub]: mhammond/pywin32 - pywin32. According to the (above) URL, the package is only designed to work on Win:
Python for Win32 (pywin32) extensions
So, this is Win specific (it was never intended for other OSes).
As for your question, technically you could install it on Linux, or OSX (it is possible with some manual steps), but that would make absolutely no difference, because (even if installed) it would NOT run on those platforms, as it contains (consists of) binaries (.dlls) that don't run (can't be loaded by processes) on non Win OSes.
Just looked at the comments: [MS.Docs]: Office.MessageCompose interface is available on OSX, but (most likely, if there isn't already a Python wrapper available) you'd have to write the bindings yourself (their examples are in JS).
Additional (generic) information
Some packages are only meant to work in specific conditions, based on restricting criteria like:
Platform(s) (OS, CPU architecture)
Python version(s)
...
even if not enforced by the installation mechanism (meaning they can somehow end up "installed" on platforms that they don't work on).
Anyway, such restrictions are (should be) mentioned (example for PyWin32 - see the red boxes) in package's:
PyPI page
Homepage
Other resources (e.g.: package's doc page, if it has one)
Bottom line is that such a package will not work outside the conditions it was meant to run in (even if it can be installed), and more: when attempting to run it, will yield some error message that might not be very clear.
There are packages with such restrictions, but can be installed outside them:
Win:
ComTypes
Error:
[cfati#cfati-5510-0:/mnt/e/Work/Dev/StackOverflow/q064975918]> python -c "import comtypes"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/cfati/.local/lib/python3.8/site-packages/comtypes/__init__.py", line 28, in <module>
from _ctypes import COMError
ImportError: cannot import name 'COMError' from '_ctypes' (/usr/lib/python3.8/lib-dynload/_ctypes.cpython-38-x86_64-linux-gnu.so)
Fix (improvement): [GitHub]: enthought/comtypes - Improve error message on non Win
WConio2
Error:
[cfati#cfati-5510-0:/mnt/e/Work/Dev/StackOverflow/q064975918]> python -c "import WConio2"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/cfati/.local/lib/python3.8/site-packages/WConio2.py", line 125, in <module>
from ctypes import windll
ImportError: cannot import name 'windll' from 'ctypes' (/usr/lib/python3.8/ctypes/__init__.py)
Fix (improvement): [GitHub]: Solomoriah/WConio2 - Improve error message on non Win

Wx not importing

So I'm using python 3.3 and recently I have tried to install wxpython and wxwidgets.
Synaptic package manager shows that wxversion, wxtools and wxgtk2.8 are all installed. When I use the python idle I get an import error however when I use iPython (command line with auto complete etc...) it happily runs the code.
What's going on?
Error Message:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module> import wx
ImportError: No module named 'wx'
IPython must be running Python 2.x because wxPython 2.8.x ONLY works with Python 2.x. If you want to use wxPython in Python 3, you have to use wxPython 2.9-Phoenix. Please note the Phoenix is kind of alpha in that ONLY the core widgets are currently available in it.
You can read more about it and download it from here:
http://wiki.wxpython.org/ProjectPhoenix
in your python shell at the very top, it will tell you what bit your python runs on. It's either 32bit or 64 bit. now once you have that info wxpython has two different bit varients depending on what python version your running, again either 32bit or 64 bit. If you have the python 32bit version you must use the wxpthon 32bit for the import to work.
I had this mistake also and figured it out this way. Good Luck!!
First let's See what version of Python we're running:
python --version
Looks like we have Python running version 3.
Python 3.3.0
That won't work.
Remove Python 3:
sudo apt-get remove python3
Let's reinstall Python but version 2.7:
sudo apt-get install python2.7 wx-common python-wxtools python-wxgtk3.0

Modules in Python on Windows

I have a simple sode snippet that uses cairomodule. I would like to launch the scipt but it somehow cannot run giving this error :
Traceback (most recent call last):
File "C:\Users\xxx\Desktop\test.py", line 1, in <module>
import cairo
ImportError: No module named cairo
how should I "install" a module in Python on Windows ? I have tried installing http://gladewin32.sourceforge.net/ as stated on cairo page that I can get cairo as a "side effect" of intalling GTK+ but somehow this didn't work.
If I should add some paths to PATH please indicate which one as I am raelly confused about this.
Now I realise how simpler that would be on linux with powerful terminal :)
Try installing PyGtk from here: http://www.pygtk.org/downloads.html. The all-in-one-installer worked for me. Just choose the packages you want installed.

Categories

Resources