如何将sqlite3模块添加到Python?

问题:如何将sqlite3模块添加到Python?

有人可以告诉我如何在最新版本的Python上安装sqlite3模块吗?我正在使用Macbook,并在命令行上尝试了:

pip install sqlite

但是会弹出一个错误。

Can someone tell me how to install the sqlite3 module alongside the most recent version of Python? I am using a Macbook, and on the command line, I tried:

pip install sqlite

but an error pops up.


回答 0

您不需要安装sqlite3模块。它包含在标准库中(自Python 2.5起)。

You don’t need to install sqlite3 module. It is included in the standard library (since Python 2.5).


回答 1

我有python 2.7.3,这解决了我的问题:

pip install pysqlite

I have python 2.7.3 and this solved my problem:

pip install pysqlite

回答 2

对于Python版本3:

pip install pysqlite3 

For Python version 3:

pip install pysqlite3 

回答 3

通常包括在内。但是,正如@ n​​gn999所说,如果您的python是从源代码手动构建的,则必须添加它。

这是一个脚本示例,该脚本将在用户目录中使用sqlite3封装版本设置Python3封装版本(虚拟环境)

INSTALL_BASE_PATH="$HOME/local"
cd ~
mkdir build
cd build
[ -f Python-3.6.2.tgz ] || wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar -zxvf Python-3.6.2.tgz

[ -f sqlite-autoconf-3240000.tar.gz ] || wget https://www.sqlite.org/2018/sqlite-autoconf-3240000.tar.gz
tar -zxvf sqlite-autoconf-3240000.tar.gz

cd sqlite-autoconf-3240000
./configure --prefix=${INSTALL_BASE_PATH}
make
make install

cd ../Python-3.6.2
LD_RUN_PATH=${INSTALL_BASE_PATH}/lib configure
LDFLAGS="-L ${INSTALL_BASE_PATH}/lib"
CPPFLAGS="-I ${INSTALL_BASE_PATH}/include"
LD_RUN_PATH=${INSTALL_BASE_PATH}/lib make
./configure --prefix=${INSTALL_BASE_PATH}
make
make install

cd ~
LINE_TO_ADD="export PATH=${INSTALL_BASE_PATH}/bin:\$PATH"
if grep -q -v "${LINE_TO_ADD}" $HOME/.bash_profile; then echo "${LINE_TO_ADD}" >> $HOME/.bash_profile; fi
source $HOME/.bash_profile

为什么这样 对于独立的开发环境,您可能需要一个模块化的python环境,您可以在不影响操作系统的情况下完全销毁和重建它。在这种情况下,解决方案就是也要模块化安装sqlite3。

Normally, it is included. However, as @ngn999 said, if your python has been built from source manually, you’ll have to add it.

Here is an example of a script that will setup an encapsulated version (virtual environment) of Python3 in your user directory with an encapsulated version of sqlite3.

INSTALL_BASE_PATH="$HOME/local"
cd ~
mkdir build
cd build
[ -f Python-3.6.2.tgz ] || wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar -zxvf Python-3.6.2.tgz

[ -f sqlite-autoconf-3240000.tar.gz ] || wget https://www.sqlite.org/2018/sqlite-autoconf-3240000.tar.gz
tar -zxvf sqlite-autoconf-3240000.tar.gz

cd sqlite-autoconf-3240000
./configure --prefix=${INSTALL_BASE_PATH}
make
make install

cd ../Python-3.6.2
LD_RUN_PATH=${INSTALL_BASE_PATH}/lib configure
LDFLAGS="-L ${INSTALL_BASE_PATH}/lib"
CPPFLAGS="-I ${INSTALL_BASE_PATH}/include"
LD_RUN_PATH=${INSTALL_BASE_PATH}/lib make
./configure --prefix=${INSTALL_BASE_PATH}
make
make install

cd ~
LINE_TO_ADD="export PATH=${INSTALL_BASE_PATH}/bin:\$PATH"
if grep -q -v "${LINE_TO_ADD}" $HOME/.bash_profile; then echo "${LINE_TO_ADD}" >> $HOME/.bash_profile; fi
source $HOME/.bash_profile

Why do this? You might want a modular python environment that you can completely destroy and rebuild without affecting your operating system–for an independent development environment. In this case, the solution is to install sqlite3 modularly too.