【问题标题】:Install tkinter and python locally在本地安装 tkinter 和 python
【发布时间】:2012-08-10 12:11:52
【问题描述】:

我在服务上使用 linux。而且我没有root权限。我将 python-3.2.3 本地安装到“/home/sam/install_sam”。当我导入 tkinter 模块时。我收到以下错误:

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

我知道我需要安装 Tkinter 模块。因为我没有root权限。我不能使用如下命令:

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

然后我在 goolge 上搜索。 我从 here 获得 tcl/tk。我使用以下命令安装它们。

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

我仍然收到错误:INFO:找不到 Tcl/Tk 库和/或标头。我应该如何为 python 配置 tcl/tk

【问题讨论】:

    标签: python linux tkinter tcl tk


    【解决方案1】:

    在构建 Python 3 之前,使用 CPPFLAGS 环境变量设置 tcl 和 tk 的包含目录。这对我有用。

    export CPPFLAGS="-I/home/sam/install_sam/tcl/include -I/home/sam/install_sam/tk/include"
    

    【讨论】:

      【解决方案2】:

      最后。我将 tcl/tk 和 python 安装在同一路径中。它现在可以工作了。命令如下:

      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
      

      有人可以告诉我如何以第一种方式为python配置tcl/tk(问题中提到)。我会很感激的

      【讨论】:

        【解决方案3】:
        sudo apt-get install tcl-dev tk-dev
        

        为我工作,虽然我最终拉了一个 docker 图像并使用它。

        【讨论】:

          【解决方案4】:

          在我的情况下,import tkinter 在我的 Python3 环境中正常工作,但我必须使用预编译的 Python 和它自己的环境(Blender fyi),它不包含依赖项(我需要 tkinter 来运行 @ 987654322@).

          在我的情况下修复非常简单:

          1. working python 中,import tkinter 并使用tkinter.__file__ 检查它的安装位置。这将类似于path/to/site-packages/tkinter

          2. tkinter 文件夹复制到目标安装的site-packages

          3. 那么import _tkinter 将不起作用。再次使用文件技巧,找到丢失的 .so 文件,在我的 Ubuntu 中类似于 `path/to/python3.7/lib-dynload/_tkinter.cpython-37m-x86_64-linux-gnu.so'

          4. 同样,将.so 文件复制到目标安装对应的lib-dynload 中。 确保源 Python 版本和目标 Python 版本兼容

          要确保您的目标 python 找到复制的文件,请确保目标路径列在 sys.path 下。

          希望这会有所帮助!
          干杯,
          安德烈斯

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-09-27
            • 2011-06-14
            • 2017-12-31
            • 2019-11-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多