I have the issue with Python 3.9.1 on freshly installed Ubuntu (Ubuntu 20.04.1 LTS) - when running any tkinter-based application, I get import error (example from python console):
user#ubuntu:~$ python3
Python 3.9.1 (default, Dec 29 2020, 13:25:02)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/linuxbrew/.linuxbrew/Cellar/python#3.9/3.9.1_3/lib/python3.9/tkinter/__init__.py", line 37, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I know this issue was discussed multiple times in SO and other internet resources, for several days I am trying all the options and solutions (like this, this, another one, brew forum neither of them solved the issue).
What works - only uninstalling homebrew does the trick. Still have hope to configure everything to work with homebrew.
What I have tried/performed:
fixed all brew doctor reported issues:
Your system is ready to brew.
tcl-tk is listed under brew list:
black hello libx11 libxdmcp openssl#1.1 pylint sqlite xorgproto
bzip2 libffi libxau libxext patchelf python#3.9 tcl-tk xz
gdbm libpthread-stubs libxcb ncurses pkg-config readline unzip zlib
performed brew uninstall python and brew install python
Initial thought was poetry and/or pyenv broke the dependencies, currently using neither of them.
I am not hardcore ubuntu user, am I missing something? Any idea or help would be valuable.
Homebrew is not the official pcakage mamanger for ubuntu and has some strange behaviours ,Ubuntu's officialy supported pacakage manager that is installed by default is apt(advanced package tool)
First uninstall your brew install of python by typing:
brew uninstall python
Then this is how you use APT to install python3:
sudo apt-get install python3
OR
sudo apt install python3
And to test if python3 installed correctly type:
python3
If a python shell opens python3 has been installed properly
Then to test if tkinter is working type this in the python shell:
import tkinter
Closing the question, as no other activity for over a week.
Used solution I wanted to avoid - removed homebrew, now Tkinter is detected properly.
For anyone stumbling on the same issue - uninstalling homebrew: https://github.com/homebrew/install#uninstall-homebrew
Related
I tried to use the matplotlib package via Pycharm IDE on windows 10.
when I run this code:
from matplotlib import pyplot
I get the following error:
ImportError: No module named 'tkinter'
I know that in python 2.x it was called Tkinter, but that is not the problem - I just installed a brand new python 3.5.1.
EDIT: in addition, I also tried to import 'tkinter' and 'Tkinter' - neither of these worked (both returned the error message I mentioned).
For Linux
Debian based distros:
sudo apt-get install python3-tk
RPM based distros:
sudo yum install python3-tkinter
For windows:
For Windows, I think the problem is you didn't install complete Python package. Since Tkinter should be shipped with Python out of box. See: http://www.tkdocs.com/tutorial/install.html . Good python distributions for Windows can be found by the companies Anaconda or ActiveState.
Test the python module
python -c "import tkinter"
p.s. I suggest installing ipython, which provides powerful shell and necessary packages as well.
you can use
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
if you dont want to use tkinter at all.
Also dont forget to use %matplotlib inline at the top of your notebook if using one.
EDIT: agg is a different backend like tkinter for matplotlib.
For Windows users, there's no need to download the installer again. Just do the following:
Go to start menu, type Apps & features,
Search for "python" in the search box,
Select the Python version (e.g. Python 3.8.3rc1(32-bit)) and click Modify,
On the Modify Setup page click Modify,
Tick td/tk and IDLE checkbox (which installs tkinter) and click next.
Wait for installation and you're done.
On Centos, the package names and commands are different. You'll need to do:
sudo yum install tkinter
To fix the problem.
Almost all answers I searched for this issue say that Python on Windows comes with tkinter and tcl already installed, and I had no luck trying to download or install them using pip, or actviestate.com site. I eventually found that when I was installing python using the binary installer, I had unchecked the module related to TCL and tkinter. So, I ran the binary installer again and chose to modify my python version by this time selecting this option. No need to do anything manually then. If you go to your python terminal, then the following commands should show you version of tkinter installed with your Python:
import tkinter
import _tkinter
tkinter._test()
If you are using fedora then first install tkinter
sudo dnf install python3-tkinter
I don't think you need to import tkinter afterwards
I also suggest you to use virtualenv
$ python3 -m venv myvenv
$ source myvenv/bin/activate
And add the necessary packages using pip
On CentOS 7 and Python 3.4, the command is sudo yum install python34-tkinter
On Redhat 7.4 with Python 3.6, the command is sudo yum install rh-python36-python-tkinter
For windows users, re-run the installer. Select Modify. Check the box for tcl/tk and IDLE. The description for this says "Installs tkinter"
On Ubuntu, early 2018, there is no python3.6-tk on ubuntu's (xenial/16.04) normal distributions, so even if you have earlier versions of python-tk this won't work.
My solution was to use set everything up with python 3.5:
sudo apt install python3.5-tk
virtualenv --python=`which python3.5` python-env
source python-env/bin/activate
pip install -r requirements.txt
And now matplotlib can find tkinter.
EDIT:
I just needed 3.6 afterall, and the trick was to:
sudo apt install tk-dev
and then rebuild python3.6, after tk-dev, eg:
./configure
make
make install
If you are using python 3.6, this worked for me:
sudo apt-get install python3.6-tk
instead of
sudo apt-get install python3-tk
Which works for other versions of python3
For the poor guys like me using python 3.7. You need the python3.7-tk package.
sudo apt install python3.7-tk
$ python
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> exit()
Note. python3-tk is installed. But not python3.7-tk.
$ sudo apt install python3.7-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
tix python3.7-tk-dbg
The following NEW packages will be installed:
python3.7-tk
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 143 kB of archives.
After this operation, 534 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]
Fetched 143 kB in 0s (364 kB/s)
Selecting previously unselected package python3.7-tk:amd64.
(Reading database ... 256375 files and directories currently installed.)
Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...
Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2) ...
Setting up python3.7-tk:amd64 (3.7.4-1+xenial2) ...
After installing it, all good.
$ python3
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()
On CentOS 6.5 with python 2.7 I needed to do: yum install python27-tkinter
Sometimes (for example in osgeo4w distribution) tkinter is removed.
Try changing matplotlib backend editing matplotlibrc file located in [python install dir]/matplotlib/mpl-data/matplotlibrc changing The backend parameter from backend: TkAgg to something other like backend: Qt4Aggas described here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
Since I'm using Python 3.7 on Ubuntu I had to use:
sudo apt-get install python3.7-tk
Maybe you installed python from source. In this case, you can recompile python with tcl/tk supported.
Complie and install tcl/tk from http://www.tcl.tk/software/tcltk/download.html, I'll suppose you installed python at /home/xxx/local/tcl-tk/.
# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
Recompile python with tcl/tk supported, for example:
# download the source code of python and decompress it first.
cd <your-python-src-dir>
./configure --prefix=/home/xxx/local/python \
--with-tcltk-includes=/home/xxx/local/tcl-tk/include \
--with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make
make install
I had the same issue on Win x86/64 because my custom Python3.7 installation did not include Tcl packages, so just modify or re-install your python
https://www.python.org/downloads/release/python-370/
Download Python Setup file and click modify then tick the tcl/tk and install.
After installation is complete go to folder where python is installed ( Default is C:\Users*Your username*\AppData\Local\Programs\Python\Python39\Lib) .
Copy the tkinter folder and paste it in the lib folder of your pycharm project.
Error should be resolved
Follow these steps to easily install Tkinter on your PyCharm IDE:
First go to the File:
Second is go to New Project Setup > Settings for new projects:
And then click the Settings for new projects and you'll get redirected in here:
Please click the + symbol in there:
after that install future and there you go...
If you’re having pip(which you probably do), open up cmd or powershell on Windows or a terminal window on OS X or Linux and try this(make sure python is in the system path if you’re on Windows):
pip install tkinter
It should take a while to install tkinter, and then try to execute this code block:
from tkinter import *
root = Tk()
# Your code goes here
root.mainloop()
Hope that this helps! Thank you!
First let me start by giving an example of what happens when I install numpy using HomeBrew
brew install numpy
This successfully dowloads and pours. And now when I try to import in python it works properly.
>>>import numpy
>>>
Now I try to install pygtk through HomeBrew using
brew install pygtk
It successfully Downloads and pours. But when I import it in python I get
>>>import pygtk
Import Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pygtk
>>>
Now I went and checked my /usr/local/lib/python2.7/site-packages/
numpy exists but pygtk does not. But pygtk exists in /usr/local/lib/
Why is brew doing this? Also am I getting something fundamentally wrong here?
Now I really don't know what to do exactly but I don't think copying pygtk from /usr/local/lib/ to /usr/local/lib/python2.7/site-packages/
Note: I am using MAC OS and I mainly work through the terminal and would prefer a solution through terminal itself.
Edit1: I have found a solution or rather a workaround.
Basically I am downloading the latest PyGTK.pkg and then installing it. And then copying all the files in the system default python to my python2.7
wget -P /Users/mac_test/Downloads/ https://sourceforge.net/projects/macpkg/files/PyGTK/2.24.0/PyGTK.pkg
installer -pkg /Users/mac_test/Downloads/PyGTK.pkg -target /
cp -r /opt/gtk/lib/python2.7/site-packages/ /usr/local/lib/python2.7/site-packages/
Not sure this is the best way though.
How it worked for me:
First of all I have python installed via brew:
which python && python --version
/usr/local/bin/python
Python 2.7.13
Then I installed pygtk through brew install pygtk and it was successful
After that:
python
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygtk
>>> help(pygtk)
It responds with this:
Help on module pygtk:
NAME
pygtk
FILE
/usr/local/lib/python2.7/site-packages/pygtk.py
So probably copy pygtk to site-packages might solve your issue.
I don't know specific reason you prefer brew over pip + virtualenv but I gently recommend you to try it instead of brew and global library installation.
After install Scrapy via pip, and having Python 2.7.10:
scrapy
Traceback (most recent call last):
File "/usr/local/bin/scrapy", line 7, in <module>
from scrapy.cmdline import execute
File "/Library/Python/2.7/site-packages/scrapy/__init__.py", line 48,
in <module>
from scrapy.spiders import Spider
File "/Library/Python/2.7/site-packages/scrapy/spiders/__init__.py",
line 10, in <module>
from scrapy.http import Request
File "/Library/Python/2.7/site-packages/scrapy/http/__init__.py", line
12, in <module>
from scrapy.http.request.rpc import XmlRpcRequest
File "/Library/Python/2.7/site-packages/scrapy/http/request/rpc.py",
line 7, in <module>
from six.moves import xmlrpc_client as xmlrpclib
ImportError: cannot import name xmlrpc_client
But I can import module:
Python 2.7.10 (default, Jun 10 2015, 19:42:47)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import scrapy
>>>
What's going on?
I've just fixed this issue on my OS X.
Please backup your files first.
sudo rm -rf /Library/Python/2.7/site-packages/six*
sudo rm -rf /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six*
sudo pip install six
Scrapy 1.0.0 is ready to go.
If you encounter an error rm: /System/Library/... Operation not permitted
Please try to disable System Integrity Protection
See Operation Not Permitted when on root El capitan (rootless disabled)
This is a known issue on Mac OSX for Scrapy. You can refer to this link.
Basically the issue is with the PYTHONPATH in your system. To solve the issue change the current PYTHONPATH to point to the newer or none Mac OSX version of Python. Before running Scrapy, try:
export PYTHONPATH=/Library/Python/2.7/site-packages:$PYTHONPATH
If that worked you can change the .bashrc file permanently:
echo "export PYTHONPATH=/Library/Python/2.7/site-packages:$PYTHONPATH" >> ~/.bashrc
If none of this works, take a look at the link above.
I had the same exact problem when upgrading to Scrapy 1.0. After numerous work arounds the solution that worked for me was uninstalling six with pip:
sudo pip uninstall six
then re-installing six via easy_install
easy_install six
Hope that works!
Better than downgrading Scrapy it's to upgrade your version of six:
pip install --upgrade six scrapy
This will allow you to use all the goodies from Scrapy 1.0 ;)
I just had the same issue. Try this:
sudo pip uninstall scrapy
sudo pip install scrapy==0.24.2
Then give it a shot
I believe the best solution on OS X should be "Don’t use system python". It will make life easier. This link shows how to do this.
There’s a known issue that prevents pip from updating system packages. This has to be addressed to successfully install Scrapy and its dependencies. Here are some proposed solutions:
(Recommended) Don’t use system python, install a new, updated version that doesn’t conflict with the rest of your system. Here’s how to do it using the homebrew package manager:
Install homebrew following the instructions in http://brew.sh/
Update your PATH variable to state that homebrew packages should be used before system packages (Change .bashrc to .zshrc accordantly if you’re using zsh as default shell):
echo "export PATH=/usr/local/bin:/usr/local/sbin:$PATH" >> ~/.bashrc
Reload .bashrc to ensure the changes have taken place:
source ~/.bashrc
Install python:
brew install python
Latest versions of python have pip bundled with them so you won’t need to install it separately. If this is not the case, upgrade python:
brew update; brew upgrade python
Hi I'm using Ubuntu release 12.10 (quantal) 32-bit with Linux Kernel 3.5.0-21-generic. I'm trying to get IPython's History to work. I've set it up using pythonbrew and a virtual environment. In there I use pip to install IPython. Currently, when I start up IPython in a terminal I get:
WARNING: IPython History requires SQLite, your history will not be saved
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Searching on the warning in the first line, I found this issue report, so I went back and installed the following:
sudo apt-get install libsqlite0 libsqlite0-dev libsqlite3-0 libsqlite3-dev
and then removed and reinstalled pysqlite using pip
pip uninstall pysqlite
pip install pysqlite
After that I thought I would check the installation by importing the module:
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
So now it seems the file _sqlite3.so can't be found. That's when I found this SO question. Either it doesn't exist or it's not in my PYTHONPATH environment variable. Searching for the file, I get:
$ locate _sqlite3.so
/home/me/Desktop/.dropbox-dist/_sqlite3.so
/home/me/epd/lib/python2.7/lib-dynload/_sqlite3.so
/usr/lib/python2.7/lib-dynload/_sqlite3.so
So the file is there, but when I looked in my python path:
import sys
for p in sys.path:
print p
none of the above paths that contain _sqlite3.so were contained in my PYTHONPATH. For giggles, I added the path /usr/lib/python2.7/lib-dynload to my PYTHONPATH in a terminal and then tried to import sqlite3 again:
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/usr/lib/python2.7/lib-dynload")
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: /usr/lib/python2.7/lib-dynload/_sqlite3.so: undefined symbol: PyUnicodeUCS4_DecodeUTF8
Uh oh. Now I'm completely stuck. Can anyone help me out? I've also read in a few places that I may have to rebuild Python. I have no idea how to do this in pythonbrew. Can anyone point me in the right direction?
I've also read in a few places that I may have to rebuild Python.
This is correct. SQLite is part of the standard library,
and is built when you compile Python. There are a few 'optional' parts
of the standard library, which Python will simply skip (with minimal warning, unfortunately)
if the dependencies are missing at build time, and sqlite is one of these.
You should be able to just install libsqlite3-dev,
then rebuild Python and you should be set.
Keep an eye on the build messages,
as they do report which modules they are skipping due to missing dependencies.
Thanks to minrk for pointing me in the right direction. All I had to do was rebuild python. I've outlined the steps below for those that are using pythonbrew. Notice that I already installed the libsqlite3-dev package up in the question section.
First, with the proper version of python and virtual environment loaded up run the command:
$ pip freeze -l > requirements.txt
This gives us a text file list of all of the pip packages that have been installed in the virtual environment for this particular python release in pythonbrew. Then we remove the version of python from pythonbrew and reinstall it (this is the "rebuild python" step):
$ pythonbrew uninstall 2.7.3
$ pythonbrew install 2.7.3
After that, we switch over to the newly installed python version 2.7.3 and create a new virtual environment (which I've called "sci"):
$ pythonbrew switch 2.7.3
$ pythonbrew venv create sci
$ pythonbrew venv use sci
Ideally you should be able to run the command:
$ pip install -r requirements.txt
and according to this pip should reinstall all the modules that you had in the virtual environment before we clobbered that version of python (2.7.3). It didn't work for me for whatever reason so I manually installed all of the modules using pip individuality.
$ ipython --pylab
Python 2.7.3 (default, Jan 5 2013, 18:48:27)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
and IPython history works!
What worked for me (using osx + homebrew + brewed python):
# Reinstall Python 2.7 with sqlite
brew remove python
brew install readline sqlite gdbm --universal
brew install python --universal --framework
# Reinstall iPython with correct bindings
pip uninstall ipython
pip install ipython
And you should be good to go.
You should rebuild your python with sqlite support
sudo apt-get install libsqlite3-dev
wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
tar -xvf Python-2.7.15.tgz
cd Python-2.7.15
./configure
make
sudo make install
Recreate your virtual environment and you should be good to go
rmvirtualenv venv
mkvirtualenv -p python2 venv
workon venv
pip install -r requirements.txt
# or
pip install ipython
This warning appears on macOS when python is installed with pyenv. By default it installs python without sqlite. These commands reinstall python with sqlite support:
pyenv uninstall 3.7
CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install 3.7
I'm using Ubuntu 11.10, which came pre-installed with Python 2.7.3.
I installed the python3 package, and the python3-tk package, via apt-get install, and they worked together "out of the box".
But though I've installed the python-tk package, I can't figure out how to get Python2.7 to see it. I get the error message below when I try to import it.
import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
edit: I also ran the following based on Python-tk package not recognized in Python 2.7.3, and got:
$ file /usr/lib/libtk8.5.so.0
/usr/lib/libtk8.5.so.0: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, stripped
$ uname -a
Linux bugbot 3.0.0-23-generic-pae #39-Ubuntu SMP Thu Jul 19 19:39:19 UTC 2012 i686 i686 i386 GNU/Linux
I figured it out after way too much time spent on this problem, so hopefully I can save someone else the hassle.
I found this old bug report deemed invalid that mentioned the exact problem I was having, I had Tkinter.py, but it couldn't find the module _tkinter: http://bugs.python.org/issue8555
I installed the tk-dev package with apt-get, and rebuilt Python using ./configure, make, and make install in the Python2.7.3 directory. And now my Python2.7 can import Tkinter, yay!
I'm a little miffed that the tk-dev package isn't mentioned at all in the Python installation documentation.... below is another helpful resource on missing modules in Python if, like me, someone should discover they are missing more than _tkinter.
Building Python and more on missing modules
I had same issue with python3.4 (ImportError: No module named '_tkinter')
sudo apt-get install tk-dev
got to python source directory
./configure
make
sudo make install
The following solved the issue for me on Linux Mint 16:
sudo apt-get install tk-dev python-tk
I use virtualenv but did not want to compile python, in order to make the python support tk, python-tk should be installed.