为 Python 安装准备 _tkinter 和 sqlite3(无管理员权限)

Posted

技术标签:

【中文标题】为 Python 安装准备 _tkinter 和 sqlite3(无管理员权限)【英文标题】:Preparing _tkinter and sqlite3 for Python installation (no admin rights) 【发布时间】:2013-04-08 05:26:23 【问题描述】:

我正在尝试直接从源代码构建 Python,没有管理员权限,并且在运行之后:

export CPPFLAGS='-I/opt/sqlite-3.7.16.2/include -I/opt/tk8.6.0/include -I/opt/tcl8.6.0/include/'

export LDFLAGS='-L/opt/sqlite-3.7.16.2/lib -L/opt/tk8.6.0/lib/ -L/opt/tcl8.6.0/lib/./configure --prefix=/path_to_python-2.7.4 --enable-shared'

然后

make

我得到以下信息:

构建“_tkinter”扩展

gcc -pthread -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11/include -I。 -IInclude -I./包括 -I/opt/sqlite/sqlite-3.7.16.2/include -I/opt/tk8.6.0/include -I/opt/tcl8.6.0/include -I/usr/local/include - I/opt/python/src/Python-2.7.4/Include -I/opt/python/src/Python-2.7.4 -c /opt/python/src/Python-2.7.4/Modules/_tkinter.c - o build/temp.linux-x86_64-2.7/opt/python/src/Python-2.7.4/Modules/_tkinter.o

gcc -pthread -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11/include -I. -IInclude -I./包括 -I/opt/sqlite/sqlite-3.7.16.2/include -I/opt/tk8.6.0/include -I/opt/tcl8.6.0/include -I/usr/local/include - I/opt/python/src/Python-2.7.4/Include -I/opt/python/src/Python-2.7.4 -c /opt/python/src/Python-2.7.4/Modules/tkappinit.c - o build/temp.linux-x86_64-2.7/opt/python/src/Python-2.7.4/Modules/tkappinit.o

gcc -pthread -shared -L/opt/sqlite/sqlite-3.7.16.2/lib -L/opt/tk8.6.0/lib/ -L/opt/tcl8.6.0/lib/ -L/opt/sqlite/sqlite-3.7.16.2/lib -L/opt/tk8.6.0/lib/ -L/选择/tcl8.6.0/lib/-I。 -IInclude -I./Include -I/opt/sqlite/sqlite-3.7.16.2/include -I/opt/tk8.6.0/include -I/opt/tcl8.6.0/include build/temp.linux-x86_64-2.7 /opt/python/src/Python-2.7.4/Modules/_tkinter.o build/temp.linux-x86_64-2.7/opt/python/src/Python-2.7.4/Modules/tkappinit.o -L/usr/X11/lib -L/opt/sqlite/sqlite-3.7.16.2/lib -L/opt/tk8.6.0/lib/ -L/opt/tcl8.6.0/lib/ -L/usr/local /lib -L。 -ltk8.6 -ltcl8.6 -lX11 -lpython2.7 -o build/lib.linux-x86_64-2.7/_tkinter.so

* 警告:重命名“_tkinter”,因为导入失败:libtk8.6.so:无法打开共享对象文件:没有这样的文件或目录

奇怪的是我可以看到libtk8.6.so。正如我用LDFLAGS 指定的那样,它实际上就在/opt/tcl8.6.0/lib 下。

为什么编译失败了?

【问题讨论】:

【参考方案1】:

此问题发生在安装setup.py 时,Python 尝试导入_tkinter。如果你看一下函数build_extension,有一个块说:

imp.load_dynamic(ext.name, ext_filename)

这一行尝试动态加载_tkinter(它使用动态共享库libtk8.6.so)。因此,即使编译/链接工作正常,当 Python 测试模块时,它也会使用动态库,而我在 LD_LIBRARY_PATH 中没有 tcl/lib 或 tk/lib。一旦我添加了这些,一切都很好。

总结: 我必须通过CPPFLAGS添加以下包含路径

/path_to/sqlite3/include /path_to/tcl/include /path_to/tk/include

以下通过LDFLAGS的lib路径

/path_to/sqlite3/lib /path_to/tcl/lib /path_to/tk/lib

以及通过LD_LIBRARY_PATH的以下库路径:

/path_to/sqlite3/lib /path_to/tcl/lib /path_to/tk/lib

有了这一切,一切正常。

【讨论】:

以上是关于为 Python 安装准备 _tkinter 和 sqlite3(无管理员权限)的主要内容,如果未能解决你的问题,请参考以下文章

python tkinter与ttk

ImportError:Python 2.7.13 中没有名为 _tkinter 的模块

Python_面向对象_tkinter库

import _tkinter # 如果失败,您的 Python 可能未配置为在 python 3.8 中出现 Tk 错误

markdown ImportError:没有名为'_tkinter'的模块,请安装python3-tk包

Python学习笔记_02:使用Tkinter连接MySQL数据库实现登陆注册功能