[教程]Centos7 安装python3 pip3 sqlite3

写在前面

说明,不是我原创,只是搜索引擎的搬运工,集合里几个帖子,完成了自己的安装,给自己留个笔记

# 编译使用

 yum install -y gcc make 

# python编译依赖

yum install -y
zlib-devel libffi-devel openssl-devel bzip2-devel ncurses-devel sqlite-devel
gdbm-devel xz-devel tk-devel readline-devel

编译python3

wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tar.xz
tar -zxvf Python-3.7.5.tar.xz
mkdir /usr/local/python3 #创建编译安装目录
cd Python-3.7.5
./configure --prefix=/usr/local/python3 --enable-optimizations #不加后面这个,编译后使用时有tls的问题
make && make install

创建软连接

ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3 
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

验证是否成功

python3 -V
pip3 -V

在CentOS7上安装sqlite3

下载软件

wget https://www.sqlite.org/2019/sqlite-autoconf-3270100.tar.gz

解压

tar zxvf sqlite-autoconf-3270100.tar.gz
cd sqlite-autoconf-3270100

编译及安装

./configure -prefix=/opt/sqlite3
make && make install

成功安装,最后输出如下信息:

----------------------------------------------------------------------
Libraries have been installed in:
   /opt/sqlite3/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

信息显示sqlite3的安装路径是:/opt/sqlite3/lib

特别注意建议添加sqlite3环境变量:

add LIBDIR to the ‘LD_LIBRARY_PATH’ environment variable during execution

添加环境变量:

export LD_LIBRARY_PATH=/opt/sqlite3

验证是否能正常运行:

/opt/sqlite3/bin/sqlite3

执行上面命令,正常运行输出下面信息:

SQLite version 3.27.1 2019-02-08 13:17:39
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>
输入命令.quit退出sqlite3
sqlite> .quit

接下重新编译安装Python3

首先修改python3源码的setup.py文件的下面部分,找到sqlite_inc_paths进行修改,如下所示:

sqlite_inc_paths = [ '/usr/include',
                             '/opt/sqlite3/include',    # 添加这一行
                             '/usr/include/sqlite',
                             '/usr/include/sqlite3',
                             '/usr/local/include',
                             '/usr/local/include/sqlite',
                             '/usr/local/include/sqlite3',
                             ]

接下进入源目录重新编译python

 
cd /usr/local/python3 #进Python编译安装目录
make && make install

验证安装是否成功:

python3
import sqlite3

未报错则表示安装成功

来自 <https://zhuanlan.zhihu.com/p/50240932>

—————————————

发现了一个可以有效解决当前django必须使用sqlite3.8.3以上版本的问题

locate django将会输出大量包含django关键字的目录文件,可以看到django安装在哪些路径下,在我的系统上,需要找到/…/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py这个脚本,django判断当前使用的sqlite版本的代码就在这里,找到以下代码块,注释掉那一行代码并更改:

def check_sqlite_version():
    # if Database.sqlite_version_info < (3, 8, 3):
    if Database.sqlite_version_info < (3, 6, 3):
        raise ImproperlyConfigured(‘SQLite 3.8.3 or later is required(found %s).’ % Database.sqlite_version)

check_sqlite_version()

再次尝试,成功

来自 <http://coollsx.com/resolve-django-for-sqlite-3-8-3-or-later-is-required/>