【问题标题】:How to build python 3.3.2 with _bz2, _sqlite and _ssl from source如何使用 _bz2、_sqlite 和 _ssl 从源代码构建 python 3.3.2
【发布时间】:2013-07-09 11:05:17
【问题描述】:
我想在我的 SLE 11 (OpenSUSE) 上从头开始构建 Python 3.3.2。
在 Python 的编译过程中,我收到了模块 _bz2、_sqlite 和 _ssl 尚未编译的消息。
我通过各种搜索引擎寻找解决方案。经常说你必须用你的包管理系统安装-dev包,但我没有root权限。
我下载了缺失库的源包,但我不知道如何告诉 Python 使用这些库。有人可以帮帮我吗?
【问题讨论】:
标签:
python-3.x
sqlite
ssl
compilation
non-admin
【解决方案1】:
简短的回答是你从源代码配置这些包并将它们放在你的主目录中的某个位置,比如./configure --prefix=$HOME/opt。
路径$HOME/.local 也可能是一个不错的选择,因为许多发行版似乎已经在用户的$PATH 中包含$HOME/.local/bin(这是pip install --user 在Linux 平台上默认放置可执行文件的地方)。
然后,您通过将额外的标志传递给编译器和链接器来告诉 Python 构建过程使用该位置的库。我将使用 SQLite 的示例作为 _sqlite3 扩展名,因为这是我今天刚要做的,所以我知道这是可行的:
# build and install SQLite from the autoconf version of the "amalgamated"
# source, available at https://www.sqlite.org/download.html
cd /path/to/sqlite-source
./configure --prefix=$HOME/opt
make -j8 # 8 parallel tasks
make install
# tell the C/C++ preprocessor to also look for headers in $HOME/opt/include
export CPPFLAGS="-I$HOME/opt/include"
# tell the linker to write $HOME/opt/lib into the runtime path of any binaries
# it creates; also tell it to look for extra libraries in $HOME/opt/lib
export LDFLAGS="-Wl,-rpath=$HOME/opt/lib -Wl,-rpath=$HOME/opt/lib64 \
-L$HOME/opt/lib -L$HOME/opt/lib64"
# tell the compiler to optimize at level 3 (optional)
export CFLAGS="-O3"
# tell Python to build into your home directory; CFLAGS, CPPFLAGS, and
# LDFLAGS are automatically picked up from the environment
cd /path/to/python-source
./configure --prefix=$HOME/opt --enable-shared --enable-optimizations
make -j8 && make install
# add $HOME/opt/bin to your $PATH
cp -i ~/.bash_profile ~/.bash_profile-$(date +%Y%m%d)
echo -e '
# add homebrew software in $HOME/opt to $PATH
export PATH="$HOME/opt/bin:$PATH"' >> ~/.bash_profile
如果您像我上面所做的那样提前定义它们,那么与构建相关的环境变量(CFLAGS,et al.)必须是 exported,如果您希望 configure把它们捡起来。
您需要注销并重新登录才能使您的$PATH 更改生效。您也可以在~/.bashrc 中修改您的$PATH,但这样做也有缺点。 (实际上,两者都有缺点,但这是另一个话题。)
this SO answer 提供更多详细信息。
【解决方案2】:
我不使用那个发行版,但是 Linux Mint(它基于 Ubuntu)。
在编译 Python 3.3.2 之前,我已经安装了必要的 -dev 库:
$ sudo apt-get 安装 libssl-dev
$ sudo apt-get install libbz2-dev
...
然后我编译并安装了 Python,这些导入工作正常。
希望对你有用
莱昂