13

I work with linux on a servies. And I don't have the root privilege. I installed the python-3.2.3 locally to "/home/sam/install_sam". when I import the tkinter module. I get the following error:

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

I know I need to install the Tkinter module. because I don't have the root privilege. I can't use like the following commands:

apt-get install python-tk
sudo apt-get install python-tk

And I search on goolge. I get tcl/tk from here. I install them use the following commands.

cd ~/Downloads/tcl8.5.11/unix
./configure --prefix=/home/sam/install_sam/tcl
make
make install

cd ~/Downloads/tk8.5.11/unix
./configure --prefix=/home/sam/install_sam/tk 
            --with- tcl=/home/sam/Downloads/tcl8.5.11/unix
make
make install

cd ~/Downloads/Python3.2.3/
export LD_LIBRARY_PATH=/home/sam/install_sam/tcl/lib:/home/sam/install_sam/tk/lib
export LD_RUN_PATH=/home/sam/install_sam/tcl/lib:/home/sam/install_sam/tk/lib
./configure --prefix=/home/sam/install_sam/python 
make
make install

I still got error: INFO: Can't locate Tcl/Tk libs and/or headers. How should I config the tcl/tk for the python

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Samuel
  • 5,977
  • 14
  • 55
  • 77

5 Answers5

7

Use CPPFLAGS environment variable to set the include directories for tcl and tk before building Python 3. This has worked for me.

export CPPFLAGS="-I/home/sam/install_sam/tcl/include -I/home/sam/install_sam/tk/include"
iaswtw
  • 162
  • 1
  • 9
6

Finally. I install tcl/tk and python in a same path. It can work now. the commands as follow:

cd ~/Downloads/tcl8.5.11/unix
./configure --prefix=/home/sam/install_sam/python3
make
make install

cd ~/Downloads/tk8.5.11/unix
./configure --prefix=/home/sam/install_sam/python3
            --with-tcl=/home/sam/Downloads/tcl8.5.11/unix
make
make install

export LD_LIBRARY_PATH=/home/sam/install_sam/python3/lib
cd ~/Downloads/Python3.2.3/3
./configure --prefix=/home/sam/install_sam/python3 
make
make install

someone can tell me how to config the tcl/tk for python in the first way(mentioned in the question). I'll appreciate it

Samuel
  • 5,977
  • 14
  • 55
  • 77
2
sudo apt-get install tcl-dev tk-dev

worked for me, although I ended up pulling a docker image and using that instead.

Den-Jason
  • 2,395
  • 1
  • 22
  • 17
  • 1
    Worked for me. Though I had to recompile and re-install python again. I compiled python3,7 from source in Ubuntu16.04 since deadsnakes is no longer supported – Yonatan Simson May 08 '22 at 20:35
1

For CentOS, this is:

yum install -y tcl-devel tk-devel

Worked on CentOS 7.

In general, I find that where RHEL has *-dev, CentOS has *-devel

FSCKur
  • 920
  • 7
  • 16
0

In my case I had import tkinter properly working on my Python3 environment, but I had to use a pre-compiled Python with its own environment (Blender fyi) that didn't include the dependencies (I needed tkinter to run matplotlib).

The fix in my case was very simple:

  1. In the working python, import tkinter and check where it is installed with tkinter.__file__. This will be something like path/to/site-packages/tkinter

  2. Copy the tkinter folder into the site-packagesof your target installation

  3. Then import _tkinter won't work. Again using the file trick, locate the missing .so file, in my Ubuntu was something like `path/to/python3.7/lib-dynload/_tkinter.cpython-37m-x86_64-linux-gnu.so'

  4. Again, copy the .so file into the corresponding lib-dynload of your target installation. Make sure that both origin and target Python versions are compatible.

To make sure that your target python finds the copied files, make sure that the destination paths are listed under sys.path.

Hope this helps!
Cheers,
Andres

fr_andres
  • 5,927
  • 2
  • 31
  • 44