Install tkinter and python locally

Viewed 15045

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

5 Answers
sudo apt-get install tcl-dev tk-dev

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

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

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

Related