13

I work with Linux on a service. 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 Google. 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 an error: INFO: Can't locate Tcl/Tk libs and/or headers. How should I configure the tcl/tk for the Python interpreter?

5 Answers 5

7

Use CPPFLAGS environment variable to set the include directories for Tcl ("tcl") and Tkinter ("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"
Sign up to request clarification or add additional context in comments.

Comments

7

Finally. I install tcl/tk and the Python executable in on the same path. It can work now. The commands are 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

Comments

2

Use:

sudo apt-get install tcl-dev tk-dev

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

1 Comment

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
1

For CentOS, this is:

yum install -y tcl-devel tk-devel

It worked on CentOS 7.

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

Comments

0

In my case, I had import tkinter properly working on my Python 3 environment, but I had to use a precompiled 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-packages of your target installation

  3. Then import _tkinter won't work. Again using the file trick, locate the missing .so file. In my Ubuntu installation, it 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 interpreter finds the copied files, and make sure that the destination paths are listed under sys.path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.