On a company machine, the easiest way (if you have no permissions) is to do a custom build.
In order to build python
with tkinter
, you need to install tcl
and tk
first. I build everything with gcc
and g++
.
Download the tcl
and tk
src.tar.gz
from here (make sure tcl
and tk
have the same version): https://www.tcl.tk/software/tcltk/download.html
# Unpack tcl
mkdir tcl_install
tar -zxvf tcl* --directory tcl_install
# configure
# say the absolute path where you want to install tcl is saved in INSTALLDIR_TCL
mkdir $INSTALLDIR_TCL # our install folder
cd tcl_install/*/unix
./configure --prefix=$INSTALLDIR_TCL CC=gcc CXX=g++ --enable-threads --enable-shared
# make install
make
make install
# cleanup
rm -rf tcl_install
And the same thing for tk
, but this time specifying where you installed tcl
:
# Unpack tk
mkdir tk_install
tar -zxvf tk* --directory tk_install
# configure
# say the absolute path where you want to install tcl is saved in INSTALLDIR_TK
mkdir $INSTALLDIR_TK # our install folder
cd tk_install/*/unix
./configure --prefix=$INSTALLDIR_TK --with-tcl=$INSTALLDIR_TCL/lib CC=gcc CXX=g++ --enable-threads --enable-shared
# make install
make
make install
# cleanup
rm -rf tk_install
Now we build python (download source here). We have to specify where tcl
and tk
are installed (edit the versions in below code). Also, before invoking make
, we need to specify where the headers of tcl
and tk
are.
# Unpack python
mkdir python_install
tar -zxvf Python* --directory python_install
# configure
# say the absolute path where you want to install tcl is saved in INSTALLDIR_PYTHON
mkdir $INSTALLDIR_PYTHON # our install folder
cd python_install
./configure --prefix=$INSTALLDIR_PYTHON CC=gcc CXX=g++ --with-tcltk-includes="-I$INSTALLDIR_TCL/include -I$INSTLLDIR_TK/include" --with-tcltk-libs="$INSTALLDIR_TCL/lib/libtcl8.6.so $INSTALLDIR_TK/lib/libtk8.6.so"
# make install
export CPPFLAGS="-I$INSTALLDIR_TCL/include -I$INSTALLDIR_TK/include"
make
make install
# cleanup
rm -rf python_install
Finally, copy the tcl
and tk
library folders to python/lib
:
# copy tcl tk libs to python
cp -r tcl/lib/tcl8.6/ python/lib
cp -r tk/lib/tk8.6/ python/lib
To use the python binary with tkinter
and avoid the message cannot import tkinter
, you'll have to export the LD_LIBRARY_PATH
before starting your app:
export LD_LIBRARY_PATH=/path_to_your_python/python/tcl/lib:/path_to_your_python/python/tk/lib:$LD_LIBRARY_PATH
/path_to_your_python/python/bin/python3.7 your_app.py # or wathever your python binary is