Install tkinter and python locally

北战南征 提交于 2019-12-17 23:33:02

问题


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


回答1:


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"



回答2:


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




回答3:


sudo apt-get install tcl-dev tk-dev

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




回答4:


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



来源:https://stackoverflow.com/questions/11948295/install-tkinter-and-python-locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!