Forums

problem migrating and installing tkinter

Hi,

I want to migrate my app that I have newly uploaded. During migration this error message appears:

ImportError: No module named '_tkinter', please install the python3-tk package

Which is strange, since I never explicitly call or import tkinter application in my code. Anyway, since my application produces a lot of graphs it probably from that fact. My question is: How can I install tkinter in my virtual environment? I have tried:

pip install -U tkinter

pip install -U tk

pip install -U python3-tk

and a few more. But the response has usually only been:

Could not find a version that satisfies the requirement tk (from versions: ) No matching distribution found for tk

And as we are speaking of install commands, is it possible to install only parts of a module?

Thanks, Jan

tkinter is a windowing library. It will not work on PythonAnywhere because there is no screen to draw the windows on.

Okay, but why does it require me to load install tkinter? I never use tkinter and also I never import it in the code that I intent to upload. Where does this tkinter come into play? When I try to migrate my code, the following message appears:

Traceback (most recent call last): File "/usr/lib/python3.5/tkinter/init.py", line 36, in <module> import _tkinter ImportError: No module named '_tkinter'

[...and a whole lot of more lines...]

How do I circumvent that it tries to import tkinter? I don't need it and nowehere in the code I wrote, it appears. Thanks, Jan

Some module that you use is trying to use it. Follow the traceback backwards to find what is trying to import something that imports tkinter.

Thanks. The solution to this problem is to add

'

import matplotlib as mpl

mpl.use('Agg')

'

in the code before importing matplot.

Now I am facing another but similar problem. I cannot install scipy.stats. When I type

'

pip install -U scipy.stats

'

in my virtual environment, I get this response:

'

Collecting scipy.stats

Could not find a version that satisfies the requirement scipy.stats (from versions: ) No matching distribution found for scipy.stats

'

checking with pip list, I see that scipy (0.19.0) is installed. How can I install scipy.stats? I didn't find anything on stackoverflow.

Thanks

As far as I can tell, scipy.stats is part of scipy, you don't need to install it separately. What happens if you just do import scipy.stats in your Python code?

When I do what you suggested this error message pops up (and the same happens,when I use

from scipy import stats

instead):

import scipy.stats
  File "/home/Equinox/.virtualenvs/homepage/lib/python3.5/site-packages/scipy/__init__.py", line 105, in <module>
    from scipy._lib._version import NumpyVersion as _NumpyVersion
ImportError: No module named 'scipy._lib'

On stackoverflow, someone suggested, 'the install was broken'. Not sure what that means or what I should do about that.

[edit by admin: formatting]

Are you running that from part of a script? Or directly in a Python console? If it's happening when you run a script, try starting a bash console, then workon homepage, then python, and in the Python interpreter run import scipy.stats. You may get the same error, but if you could copy/paste the full stack trace from the console, it might be instructive.

I made a complete overhaul and installed everything anew, and the problem does not persist anymore. Thanks

That's odd! Glad you got it working, anyway.

Is it really impossible to use tkinter module in pythonanywhere?

pretty much. pythonanywhere is a server-side environment, so it doesn't have access to a GUI, and tkinter is a GUI library...

was there something specific you were trying to install that needs tkinter? sometimes there are workarounds or alternatives...

Otherwise, see our general help page on tkinter

I am trying to use matplotlib to output a png with plt.savefig(file_path) - It works on my Centos7 dev env. matplotlib.use('Agg') is not working for me and I am still getting the "No module named '_tkinter', please install the python3-tk package" error.

Has anyone gotten matplotlib to output a png on PythonAnywhere? Thanks.

Just a bit of an update. It does seem for some reason on my PA setup it is still trying to use TkAgg. Even if I set the default backend to Agg in a .mplstyle and use that it does this. edit nevermind - that was never going to work in the .mplstyle file anyway, needs to be in the home/me/.config/matplotlib lets see.... Hmmmmm.......

Fixed. Making a file called matplotlibrc containing

backend      : Agg

in the home/me/.config/matplotlib directory seems to have fixed it

Cool. Glad you worked it out.

Even though I am not using tkinterin my Django project iam getting error,why? , I have wasted all the day in deploying through pythonanywhere.

@Ramakrishnachidurala What error are you getting?