In order to build python3.4.2 from source with the _tkinter
module you need to install the following build dependency:
sudo apt-get install tk8.6-dev
Then all you have to do is running make
again to add _tkinter
support as the setup.py
file will automatically detect the tk/tcl headers and create the module:
~/Downloads/Python-3.4.2$ make
running build
running build_ext
building '_tkinter' extension
gcc -pthread -fPIC -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -DWITH_APPINIT=1 -I/usr/include/tcl8.6 -I/usr/X11/include -I./Include -I. -IInclude -I/usr/include/x86_64-linux-gnu -I/usr/local/include -I/home/sylvain/Downloads/Python-3.4.2/Include -I/home/sylvain/Downloads/Python-3.4.2 -c /home/sylvain/Downloads/Python-3.4.2/Modules/_tkinter.c -o build/temp.linux-x86_64-3.4/home/sylvain/Downloads/Python-3.4.2/Modules/_tkinter.o
gcc -pthread -fPIC -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -DWITH_APPINIT=1 -I/usr/include/tcl8.6 -I/usr/X11/include -I./Include -I. -IInclude -I/usr/include/x86_64-linux-gnu -I/usr/local/include -I/home/sylvain/Downloads/Python-3.4.2/Include -I/home/sylvain/Downloads/Python-3.4.2 -c /home/sylvain/Downloads/Python-3.4.2/Modules/tkappinit.c -o build/temp.linux-x86_64-3.4/home/sylvain/Downloads/Python-3.4.2/Modules/tkappinit.o
gcc -pthread -shared build/temp.linux-x86_64-3.4/home/sylvain/Downloads/Python-3.4.2/Modules/_tkinter.o build/temp.linux-x86_64-3.4/home/sylvain/Downloads/Python-3.4.2/Modules/tkappinit.o -L/usr/X11/lib -L/usr/lib/x86_64-linux-gnu -L/usr/local/lib -ltk8.6 -ltcl8.6 -lX11 -o build/lib.linux-x86_64-3.4/_tkinter.cpython-34m.so
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _dbm _gdbm
_lzma _sqlite3
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
[...]
Now you can import tkinter in python3.4.2:
~/Downloads/Python-3.4.2$ ./python
Python 3.4.2 (default, Oct 30 2014, 11:34:17)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
Original answer:
Unless you really need python3.4.2, I would just use the default python3 version on 14.04 (3.4.0)
Then all you have to do is installaling the following packages:
sudo apt-get install python3-tk tk
And start the python interpreter this way:
/usr/bin/python3
Otherwise you'll always get the version that you installed in /usr/local
(3.4.2).
Importing tk in python3 should work now:
$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
Pythons 2 and 3 live very happily next to each other on most Ubuntu installations. What you're describing (python3
mapping to a Python 2 binary) is not normal at all.
python
needs to be mapped through to Python 2 by default. There are various scripts that aren't Python 3 compliant (it's not backwards compatible) so if you break the mapping from python
, you break the system.
You're already seeing that from packages' postinst scripts that expect Python 2.
Here's how my python
is mapped (a 14.04 install):
$ readlink -f $(which python)
/usr/bin/python2.7
So let's just remap /usr/bin/python
back there:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python{2.7,}
Then run your sudo apt-get -f install
which should now be able to run without error.
If you've knackered things so badly, you might find yourself in the position where you need to manually unpack the Python packages into your system (they're just zips with header data).
If you've just munched /usr/bin/python2.7
(which should be the Python 2 binary) you can replace this by downloading the python2.7-minimal package and extracting the binary to the right place:
apt-get download python2.7-minimal
ar x python2.7-minimal_*.deb data.tar.xz
sudo tar xJf data.tar.xz -C / './usr/bin/python2.7'
rm data.tar.xz
That's based on the current 14.04 package. If you're on another release you might have to tweak paths. Or copy from a live system running the same version of Ubuntu.
More broadly speaking, if you want a Python development environment, I seriously suggest you look at the venv
module. This is as with Py2's VirtualEnv except that it's built-in. You can install whatever you like in a non-root, non-system way and that includes mapping python
to the Python binary you pick (including Pypy).
On 14.04 there's a bug in Python 3 that needs fixing before you can create a venv, but it that can be worked-around quite easily at the moment. We just need to install ensurepip
:
wget -qO- http://d.pr/f/YqS5+ \
| sudo tar xzf - -C $(python3 -c "import sys; print(sys.path[1])") --no-same-owner
Then create and activate the venv:
python3 -m venv myvenv
source ./myvenv/bin/activate
Now you're in your own playground. You'll need to call the activate in the future (or call the myvenv/bin/python binary explicitly) to load the right Python path.
Best Answer
Ok, so the problem is resolved (for the most part).
I checked my
$PATH
, and found that I had appended the path/home/d3ath/Enthought/Canopy_64bit/User/bin
to the start. Naturally it execute the python found in there first rather than the one in/usr/bin/
as it should. If anything the moral of the story is be careful what you do to your path.I say "for the most part" because I don't know if whatever caused the python to fail the first time will be an import package I need to execute some of my py-code. I will have to find out and correct as I go.