Tkinter in virtual environment (PyCharm/Mac OS 10.13.4) - python

I have set up a virtual environment and when I try to run matplotlib on it, I get the following error:
Traceback (most recent call last):
File "/Users/sumeet/PycharmProjects/VISM/test2.py", line 2, in <module>
import matplotlib.pyplot as plt
File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
[backend_name], 0)
File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in <module>
from . import tkagg # Paint image to Tk photo blitter extension.
File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 5, in <module>
from six.moves import tkinter as Tk
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/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
From what I understand, I need to
set the environment variables correctly in the activate script
or I can symlink tkinter folder to virtualenv python2.7.
I have followed the steps here: TKinter in a Virtualenv, but I am still getting the error.
Steps that I followed are:
To virtualenv/bin/activate, I added
TK_LIBRARY=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib
TKPATH=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib
TCL_LIBRARY=/usr/lib
export TCL_LIBRARY TK_LIBRARY TKPATH
In virtualenv/lib/python2.7 I ran, ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_tkinter.so
Note that when I am using the system Python as an interpreter on PyCharm, matplotlib works fine which gives me a feeling that Tkinter is available somewhere on my system but I am failing to call it from my virtual environment.
The link above seems to be working for Ubuntu. I am wondering if anyone know what is the analogous version for Mac.
Thanks!

If you are not specific to using tkinter in virtual environment then you can use the following lines of code:
import matplotlib
matplotlib.use(‘TkAgg’)
import matplotlib.pyplot as plt
I hope this solves the problem.

Related

import matplotlib.pyplot hangs on ubuntu

I am using a Python3 installation in a Ubuntu distribution in WSL2 on a Windows 10 machine.
Over the last two months, i did not code that much. Now i started again and faced the following issue:
The following code hangs for ~2min:
import matplotlib.pyplot
After 2 min, i got the following output:
Traceback (most recent call last):
File "a.py", line 5, in <module>
import matplotlib.pyplot as plt
File "/home/markus/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2469, in <module>
switch_backend(rcParams["backend"])
File "/home/markus/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 277, in switch_backend
class backend_mod(matplotlib.backend_bases._Backend):
File "/home/markus/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 278, in backend_mod
locals().update(vars(importlib.import_module(backend_name)))
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/markus/.local/lib/python3.8/site-packages/matplotlib/backends/backend_qt4agg.py", line 6, in <module>
from .backend_qt5agg import (
File "/home/markus/.local/lib/python3.8/site-packages/matplotlib/backends/backend_qt5agg.py", line 11, in <module>
from .backend_qt5 import (
File "/home/markus/.local/lib/python3.8/site-packages/matplotlib/backends/backend_qt5.py", line 13, in <module>
import matplotlib.backends.qt_editor.figureoptions as figureoptions
File "/home/markus/.local/lib/python3.8/site-packages/matplotlib/backends/qt_editor/figureoptions.py", line 11, in <module>
from matplotlib.backends.qt_compat import QtGui
File "/home/markus/.local/lib/python3.8/site-packages/matplotlib/backends/qt_compat.py", line 179, in <module>
raise ImportError("Failed to import any qt binding")
ImportError: Failed to import any qt binding
The issue has already been described here: import matplotlib.pyplot hangs
But the suggested solutions did not work.
I then ran
sudo pip install --upgrade matplotlib
which helped, as the import command works now, but it still takes 2 min to execute!
Do you know, why this command takes so much time?
Uninstalling and reinstalling Matlplotlib did not help.
Uninstalling and reinstalling python 3.X did not help.
What did work was resetting the Ubuntu installation in the windows settings, e.g. see here:
https://askubuntu.com/questions/761360/how-do-i-reset-my-installation-of-ubuntu-on-windows

ImportError: cannot import name 'HTTPSHandler' from 'urllib.request' for windows

I found 0 topics about this issue for Windows, only for Ubuntu, MAc etc.
When I try to install any package via Pycharm, e.g. urllib, matplotlib, heidi, I get the exact same error message as below.
Project interpreter is Anaconda Python 3.7.
Any idea how this can solved?
Error: Traceback (most recent call last):
File "E:\Download\PROGIK\Charm Community Edition 2018.3.5\helpers\packaging_tool.py", line 73, in run_pip
runpy.run_module(module_name, run_name='__main__', alter_sys=True)
File "E:\Download\PROGIK\Anaconda\lib\runpy.py", line 201, in run_module
mod_name, mod_spec, code = _get_module_details(mod_name)
File "E:\Download\PROGIK\Anaconda\lib\runpy.py", line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "E:\Download\PROGIK\Anaconda\lib\runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "E:\Download\PROGIK\Anaconda\lib\site-packages\pip\__init__.py", line 15, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "E:\Download\PROGIK\Anaconda\lib\site-packages\pip\vcs\subversion.py", line 9, in <module>
from pip.index import Link
File "E:\Download\PROGIK\Anaconda\lib\site-packages\pip\index.py", line 30, in <module>
from pip.wheel import Wheel, wheel_ext
File "E:\Download\PROGIK\Anaconda\lib\site-packages\pip\wheel.py", line 35, in <module>
from pip._vendor.distlib.scripts import ScriptMaker
File "E:\Download\PROGIK\Anaconda\lib\site-packages\pip\_vendor\distlib\scripts.py", line 14, in <module>
from .compat import sysconfig, detect_encoding, ZipFile
File "E:\Download\PROGIK\Anaconda\lib\site-packages\pip\_vendor\distlib\compat.py", line 66, in <module>
from urllib.request import (urlopen, urlretrieve, Request, url2pathname,
ImportError: cannot import name 'HTTPSHandler' from 'urllib.request' (E:\Download\PROGIK\Anaconda\lib\urllib\request.py)
Edit: with pip I have only problem with matplotlib, which is another topic:
Cannot install matplotlib. Fatal error LNK1181, Visual Studio fail
Simply went into packages folder and deleted matplotlib package manually because could not make it display graphs in Jupyter (didnt try in Pycharm). Trying to reinstall it now, hopefully it will fix the issue.
To install packages whithin anaconda environment under Windows10, you should use "Anaconda prompt" (find it in win menu)
It starts (Base) venv by default and installs packages in it. Therefore using Python interpreter distributed with Anaconda you will be able to import them with no side actions.
Besides matplotlib is installed there by default

Don't understand this No module named 'tkinter' error

I have been using Jupyter notebook in AWS EMR when I run a program which requires graphical representations. I get this error:
No module named 'tkinter'
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/seaborn/__init__.py", line
6, in <module>
from .rcmod import *
File "/usr/local/lib/python3.4/site-packages/seaborn/rcmod.py", line 5,
in <module>
from . import palettes, _orig_rc_params
File "/usr/local/lib/python3.4/site-packages/seaborn/palettes.py", line 12, in <module>
from .utils import desaturate, set_hls_values, get_color_cycle
File "/usr/local/lib/python3.4/site-packages/seaborn/utils.py", line 11, in <module>
import matplotlib.pyplot as plt
File "/usr/local/lib64/python3.4/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib64/python3.4/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
[backend_name], 0)
File "/usr/local/lib64/python3.4/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in <module>
from . import tkagg # Paint image to Tk photo blitter extension.
File "/usr/local/lib64/python3.4/site-packages/matplotlib/backends/tkagg.py", line 5, in <module>
from six.moves import tkinter as Tk
File "/usr/local/lib/python3.4/site-packages/six.py", line 92, in __get__
result = self._resolve()
File "/usr/local/lib/python3.4/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python3.4/site-packages/six.py", line 82, in _import_module
__import__(name)
ImportError: No module named 'tkinter'
You should run matplotlib with a non-interactive backend on a EC2 server as EC2 is headless. Configure your matplotlibrc file with this line.
backend : agg
To know more about this file, follow this link.
I believe you need to install the python3-tk module. If you're using a debian-based distro then run apt-get install python3-tk from the command line.
I just looked at this on a CentOS 7 VM (closest thing to your environment that I have set up right now). Because you are not using a software collection (definitely recommended), it looks like Tkinter can be installed with:
sudo yum install python33-python-tkinter.x86_64
But it looks like that is for Python 3.3 whereas you are using Python 3.4, so I'd suggest trying:
sudo yum install python34-python-tkinter.x86_64
Finding Packages
If that does not work, the easiest way I know of to find the appropriate package is to do:
yum search tkinter | less
...and see what packages that turns up.
Software Collection
If you go the route of software collections, you would need to install the appropriate Python distribution (see the Software Collections site here), then install the appropriate tkinter version. For me, yum search tkinter brings up:
...
rh-python34-python-tkinter.x86_64
rh-python35-python-tkinter.x86_64
rh-python36-python-tkinter.x86_64
...
These are for the software collections and correspond to Python 3.4, 3.5, and 3.6.

How to resolve import matplotlib.pyplot as plt error?

I am having trouble getting my matplotlib library to work. I am running it on a virual environment, and I have installed numpy, and matplotlib. I first open my virtual environment, activate python, then I enter,
import matplotlib
everything goes fine, but then I enter,
import matplotlib.pyplot as plt
I get the error,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/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
I have tried a number of different solutions to no avail. What could be the problem?
Use the below mentioned commands:
import matplotlib
matplotlib.use(‘TkAgg’)
import matplotlib.pyplot as plt
This worked for me. I hope it helps.

Pypy and Matplotlib error in virtual environment

I am using Ubuntu 16.04 and Python 2.7, and I have set up a virtual environment (virtualenv) using a pypy interpreter.
I even installed succeffully the matplotlib library.
However, we it comes to using it on my own code I get this error messagge:
Traceback (most recent call last):
File "my_script.py",
line 20, in
import matplotlib.pyplot as plt
File "/home/.../venv/site-packages/matplotlib/pyplot.py",
line 115, in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File
"/home/.../venv/site-packages/matplotlib/backends/init.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/home/.../venv/site-packages/matplotlib/backends/backend_tkagg.py",
line 13, in
import matplotlib.backends.tkagg as tkagg
File "/home/.../venv/site-packages/matplotlib/backends/tkagg.py",
line 9, in
from matplotlib.backends import _tkagg RuntimeError: Cannot dlopen tkinter module file
What can I do to fix it?
EDIT: The solution proposed here is the same, but I think that the problem (pypy and matplotlib) and the error message are quite different.

Categories