Tkinter found on Python 3 but not on Python2.7 - python-3.x

I am on CentOs7. I installed tk, tk-devel, tkinter through yum. I can import tkinter in Python 3, but not in Python 2.7. Any ideas?
Success in Python 3 (Anaconda):
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
But fail on Python 2.7 (CentOS default):
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libTix.so: cannot open shared object file: No such file or directory
I read some answers said
If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.
I have reinstalled tk, tk-devel and tkinter through yum, but the problem is same.
How can I configure it to work on Python 2.7?

For python 3 use:
import tkinter
For python 2 use:
import Tkinter
If these do not work install with, for python 3:
sudo apt-get install python3-tk
or, for python 2:
sudo apt-get install python-tk
you can find more details here

For python2.7 try
import Tkinter
With a capital T. It should already be pre-installed in default centos 7 python setup, if not do yum install tkinter

Related

No module named 'tensorflow.python.platform'

I am using python3 and have installed tensorflow-gpu using:
pip3 install tensorflow-gpu==1.13.1
And have checked that it is installed by:
pip3 show tensorflow-gpu
Name: tensorflow-gpu
Version: 1.13.1
Summary: TensorFlow is an open source machine learning framework for
everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: opensource#google.com
License: Apache 2.0
Location: /usr/lib64/python3.6/site-packages
Requires: tensorflow-estimator, keras-applications, termcolor, absl-py, six,
astor, protobuf, wheel, keras-preprocessing, gast, grpcio, numpy,
tensorboard
Required-by:
but when I do the following it gives me an error:
user:/home/mydirectory # python3
Python 3.6.5 (default, Mar 31 2018, 19:45:04) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/site-packages/tensorflow/__init__.py", line 24,
in <module>
from tensorflow.python import pywrap_tensorflow # pylint:
disable=unused-import
File "/usr/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow.py", line 25, in <module>
from tensorflow.python.platform import self_check
ModuleNotFoundError: No module named 'tensorflow.python.platform'
I see from the error that its looking for tensorflow in directory
/usr/lib/python3.6/site-packages/tensorflow
But pip indicates that its installed at
/usr/lib64/python3.6/site-packages
How can I fix this issue?
Please follow below steps..
0) please login with root permission or sudo and write this command in terminal
1)pip3 install tensorflow-gpu==1.13.1
2)pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
3)shekh#shekh:~$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
https://www.tensorflow.org/install/pip
See https://github.com/tensorflow/tensorflow/issues/374
Are you opening the terminal from within the tensorflow directory?
"Actually I had this issue because I was trying to import tensorflow from a python session inside the tensorflow repo folder, going to some other folder and starting python and importing tensorflow worked for me."
I also had this error and it was solved with this command
python2:
PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python2.7/dist-packages/"
export PYTHONPATH
python3:
PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python3.6/dist-packages/"
export PYTHONPATH
I encountered a similar error on a Windows 10 system.
There, I had a second tensorflow install under AppData\Roaming\Python\.... This install was likely a product of some previous work that I did not clean up properly, so for me, removing the content of the site-packages directory solved the issue. After that I could import tensorflow without any problems.

Trouble with installing libffi-dev for Python 3.7

When trying to install pgadmin4 in desktop mode on my Ubuntu system, I received a ModuleNotFoundError for _ctypes.
I did some research and found that _ctypes requires the libffi-dev package to be installed. However it seems that libffi-dev and thus _ctypes was installed for Python 2.7, when I run import ctypes it seems to work:
$ python2
Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>>
When I try to do the same for Python 3.7, it doesn't work:
$ python
Python 3.7.3 (default, Jun 21 2019, 12:46:58)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
How could I add the module _ctypes to my Python 3.7 configuration?
If you install python from source file, you have to install some required packages manually as mentioned in https://superuser.com/questions/1412975/how-to-build-and-install-python-3-7-x-from-source-on-debian-9-8.
Actually you are supposed to see some errors after make due to libffinot found as shown in the screenshot below. However, you can still run make install despite the error. When you open python after the installation and import the module, it then gives you such error.
To solve this problem, you can install the dependent package i.e libffi or libffi-devel(redhat) prior to ./configure, make and make install as mentioned in:
Package libffi was not found in the pkg-config search path REDHAT6.5 and
https://bugs.python.org/issue31652.

install wxpython for python2 and 3 on ubuntu

I am running Ubuntu 16 and have both python 2 and 3. I have downloaded wxpython and it works with the python2 interpreter but not 3. I get
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>>
and
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'wx'
What do I need to do to get installed for python 3?
A similar situation arises on Fedora 25, on which I was able to solve this. Your mileage may vary on Ubuntu.
First note that wxPython is available in two major versions, let's call them wx3 and wx4. You can identify your running version through import wx; print(wx.version()). The version string on Fedora 25 reads '3.0.2.0 gtk3 (classic)', i.e. a brand of wx3. On sourceforge these versions are known as 'wxPython' and 'wxPython4', and wxpython.org calls wx4 'phoenix'.
Inspecting the source code of wx3 you will note that wx3's syntax is incompatible with python3. Conversely, wx4 is compatible both with python2.7 and python3.
wx4 doesn't seem to be available on Fedora 25, therefore python3 can't run any wx out-of-the-box. Ubuntu may or may not have the same issue.
The following worked for me to install wx4 in a python3 virtual environment:
pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/fedora-26 wxPython
I presume the answer to your question would be
pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04 wxPython
In case you need to port a (py2, wx3) application to python3, you would be wise creating an intermediate step: (py2, wx3) -> (py2, wx4) -> (py3, wx4), noting that (py3, wx3) is an impossibility.
To create the (py2, wx4) environment was more cumbersome for me, because the above pip install command fails to find header files when run using pip2.
What ended up working for me was to download the 4.0.0b2 source https://pypi.python.org/packages/bc/6f/f7bb525517557e1c596bf22ef3f242b87afaeab57c9ad460cb94b3b0714e/wxPython-4.0.0b2.tar.gz#md5=2e3716205da8f52d8039095d14534bf7
and follow the build instructions https://github.com/wxWidgets/Phoenix/blob/master/README.rst , from which I only used the build command python build.py dox etg --nodoc sip build .
After building, you need to tell your python2 where to find the wx4 library. I ended up doing that by creating a virtualenv, and creating a symbolic link like so:
/home/user/venv/lib/python2.7/site-packages/wx -> /home/user/downloads/wxPython/wxPython-4.0.0b2/wx
That latter directory cointaining the result of the build.

How to give python access to system wide modules in ubuntu?

I have python 2.7.12 installed in Ubuntu 16.04 (64-bit version). I have modules such as numpy, scipy, sympy etc. installed via pip as well. My problem is, when I open python command line via Terminal and try to import these modules, I get the following error:
$ python
Python 2.7.12 (default, Jul 10 2016, 20:42:07)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
Upon doing some research, I found from this thread that if I open python command line using /usr/bin/python and try importing these modules, I don't get any errors.
$ /usr/bin/python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import scipy
>>> import sympy
>>> import matplotlib
>>> import pandas
>>>
But I would like to know if there is any way I can just type in python from Terminal and import these modules in the python command line? For example, if I write a program like this,
x = 2
print x
y = 5
print y
print x+y
import numpy
import scipy
import sympy
save it in a file named test.py in my desktop and open it using the command /usr/bin/python test.py, I am getting the desired output.
$ /usr/bin/python test.py
2
5
7
But if I try the same with the command python test.py, I get the error again
$ python test.py
2
5
7
Traceback (most recent call last):
File "test.py", line 8, in <module>
import numpy
ImportError: No module named numpy
From what I understand, python doesn't have access to system wide modules since it is installed locally. If so, is there a way to make python global or the modules local to python? I have been trying for the past couple of hours to find a solution but I haven't found anything yet and I am new to Linux. Thanks for your help.
I think the root cause is you have several python binary under $PATH, and your system doesn't use /usr/bin/python by default.
run command which python to see which python is used by default
rename the default python file to something like 'python-2-7-12'
then try to run python test.py again to see if it is resolved.

No argparse module shipping with python 3.1.2 on Mint 10

Why is this happening:
$ python3
Python 3.1.2 (release31-maint, Dec 9 2011, 20:50:50)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named argparse
I installed python3 precisely because I wanted to use argparse, which was new in 2.7, and the default version on Ubuntu Server 10.04 is 2.6.
Another thing I've noticed: Mint 10 comes with python 2.6.6, which does include argparse, and the exact version that ships with ubuntu-server 10.04 is 2.6.5 which does not have argparse. Also, I've noticed that on my Ubuntu 12.04 machine python3 is 3.2.3 and this does come with argparse. Why would this module not be included with 3.1.x???
Although argparse only made it into the standard lib on Python 2.7 and Python 3.2, for older/other versions you can still get it from pypi.
Keep in mind though that this version may not include all the updates that happened since the merge of argparse to the standard lib (as it's explained on the original project's webpage).

Resources