簡體   English   中英

在本地安裝 tkinter 和 python

[英]Install tkinter and python locally

我在服務上使用 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上搜索。 我從這里得到 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: Can't locate Tcl/Tk libs and/or headers 我應該如何為 python 配置 tcl/tk

在構建 Python 3 之前,使用 CPPFLAGS 環境變量設置 tcl 和 tk 的包含目錄。這對我有用。

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

最后。 我將 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(在問題中提到) 我會很感激

sudo apt-get install tcl-dev tk-dev

為我工作,雖然我最終拉了一個 docker 圖像並使用它。

在我的情況下,我已經import tkinter tkinter 在我的 Python3 環境中正常工作,但我必須使用預編譯的 Python 和它自己的不包含依賴項的環境(Blender fyi)(我需要 tkinter 來運行matplotlib )。

在我的情況下,修復非常簡單:

  1. 工作python 中, import tkinter 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下。

希望這可以幫助!
干杯,
安德烈斯

對於 CentOS,這是:

yum install -y tcl-devel tk-devel

在 CentOS 7 上工作。

一般來說,我發現 RHEL 有*-dev ,CentOS 有*-devel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM